Search in sources :

Example 41 with WifiDisplay

use of android.hardware.display.WifiDisplay in project android_frameworks_base by ParanoidAndroid.

the class PersistentDataStore method loadRememberedWifiDisplaysFromXml.

private void loadRememberedWifiDisplaysFromXml(XmlPullParser parser) throws IOException, XmlPullParserException {
    final int outerDepth = parser.getDepth();
    while (XmlUtils.nextElementWithin(parser, outerDepth)) {
        if (parser.getName().equals("wifi-display")) {
            String deviceAddress = parser.getAttributeValue(null, "deviceAddress");
            String deviceName = parser.getAttributeValue(null, "deviceName");
            String deviceAlias = parser.getAttributeValue(null, "deviceAlias");
            if (deviceAddress == null || deviceName == null) {
                throw new XmlPullParserException("Missing deviceAddress or deviceName attribute on wifi-display.");
            }
            if (findRememberedWifiDisplay(deviceAddress) >= 0) {
                throw new XmlPullParserException("Found duplicate wifi display device address.");
            }
            mRememberedWifiDisplays.add(new WifiDisplay(deviceAddress, deviceName, deviceAlias));
        }
    }
}
Also used : WifiDisplay(android.hardware.display.WifiDisplay) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 42 with WifiDisplay

use of android.hardware.display.WifiDisplay in project android_frameworks_base by ParanoidAndroid.

the class WifiDisplayAdapter method fixRememberedDisplayNamesFromAvailableDisplaysLocked.

private void fixRememberedDisplayNamesFromAvailableDisplaysLocked() {
    // It may happen that a display name has changed since it was remembered.
    // Consult the list of available displays and update the name if needed.
    // We don't do anything special for the active display here.  The display
    // controller will send a separate event when it needs to be updates.
    boolean changed = false;
    for (int i = 0; i < mRememberedDisplays.length; i++) {
        WifiDisplay rememberedDisplay = mRememberedDisplays[i];
        WifiDisplay availableDisplay = findAvailableDisplayLocked(rememberedDisplay.getDeviceAddress());
        if (availableDisplay != null && !rememberedDisplay.equals(availableDisplay)) {
            if (DEBUG) {
                Slog.d(TAG, "fixRememberedDisplayNamesFromAvailableDisplaysLocked: " + "updating remembered display to " + availableDisplay);
            }
            mRememberedDisplays[i] = availableDisplay;
            changed |= mPersistentDataStore.rememberWifiDisplay(availableDisplay);
        }
    }
    if (changed) {
        mPersistentDataStore.saveIfNeeded();
    }
}
Also used : WifiDisplay(android.hardware.display.WifiDisplay)

Example 43 with WifiDisplay

use of android.hardware.display.WifiDisplay in project android_frameworks_base by ParanoidAndroid.

the class WifiDisplayAdapter method requestRenameLocked.

public void requestRenameLocked(String address, String alias) {
    if (DEBUG) {
        Slog.d(TAG, "requestRenameLocked: address=" + address + ", alias=" + alias);
    }
    if (alias != null) {
        alias = alias.trim();
        if (alias.isEmpty() || alias.equals(address)) {
            alias = null;
        }
    }
    WifiDisplay display = mPersistentDataStore.getRememberedWifiDisplay(address);
    if (display != null && !Objects.equal(display.getDeviceAlias(), alias)) {
        display = new WifiDisplay(address, display.getDeviceName(), alias);
        if (mPersistentDataStore.rememberWifiDisplay(display)) {
            mPersistentDataStore.saveIfNeeded();
            updateRememberedDisplaysLocked();
            scheduleStatusChangedBroadcastLocked();
        }
    }
    if (mActiveDisplay != null && mActiveDisplay.getDeviceAddress().equals(address)) {
        renameDisplayDeviceLocked(mActiveDisplay.getFriendlyDisplayName());
    }
}
Also used : WifiDisplay(android.hardware.display.WifiDisplay)

Example 44 with WifiDisplay

use of android.hardware.display.WifiDisplay in project android_frameworks_base by ParanoidAndroid.

the class WifiDisplayController method updateConnection.

/**
     * This function is called repeatedly after each asynchronous operation
     * until all preconditions for the connection have been satisfied and the
     * connection is established (or not).
     */
private void updateConnection() {
    // have disconnected from the old one.
    if (mRemoteDisplay != null && mConnectedDevice != mDesiredDevice) {
        Slog.i(TAG, "Stopped listening for RTSP connection on " + mRemoteDisplayInterface + " from Wifi display: " + mConnectedDevice.deviceName);
        mRemoteDisplay.dispose();
        mRemoteDisplay = null;
        mRemoteDisplayInterface = null;
        mRemoteDisplayConnected = false;
        mHandler.removeCallbacks(mRtspTimeout);
        mWifiP2pManager.setMiracastMode(WifiP2pManager.MIRACAST_DISABLED);
        setRemoteSubmixOn(false);
        unadvertiseDisplay();
    // continue to next step
    }
    // Step 2. Before we try to connect to a new device, disconnect from the old one.
    if (mDisconnectingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    if (mConnectedDevice != null && mConnectedDevice != mDesiredDevice) {
        Slog.i(TAG, "Disconnecting from Wifi display: " + mConnectedDevice.deviceName);
        mDisconnectingDevice = mConnectedDevice;
        mConnectedDevice = null;
        unadvertiseDisplay();
        final WifiP2pDevice oldDevice = mDisconnectingDevice;
        mWifiP2pManager.removeGroup(mWifiP2pChannel, new ActionListener() {

            @Override
            public void onSuccess() {
                Slog.i(TAG, "Disconnected from Wifi display: " + oldDevice.deviceName);
                next();
            }

            @Override
            public void onFailure(int reason) {
                Slog.i(TAG, "Failed to disconnect from Wifi display: " + oldDevice.deviceName + ", reason=" + reason);
                next();
            }

            private void next() {
                if (mDisconnectingDevice == oldDevice) {
                    mDisconnectingDevice = null;
                    updateConnection();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // to the old one.
    if (mCancelingDevice != null) {
        // wait for asynchronous callback
        return;
    }
    if (mConnectingDevice != null && mConnectingDevice != mDesiredDevice) {
        Slog.i(TAG, "Canceling connection to Wifi display: " + mConnectingDevice.deviceName);
        mCancelingDevice = mConnectingDevice;
        mConnectingDevice = null;
        unadvertiseDisplay();
        mHandler.removeCallbacks(mConnectionTimeout);
        final WifiP2pDevice oldDevice = mCancelingDevice;
        mWifiP2pManager.cancelConnect(mWifiP2pChannel, new ActionListener() {

            @Override
            public void onSuccess() {
                Slog.i(TAG, "Canceled connection to Wifi display: " + oldDevice.deviceName);
                next();
            }

            @Override
            public void onFailure(int reason) {
                Slog.i(TAG, "Failed to cancel connection to Wifi display: " + oldDevice.deviceName + ", reason=" + reason);
                next();
            }

            private void next() {
                if (mCancelingDevice == oldDevice) {
                    mCancelingDevice = null;
                    updateConnection();
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // Step 4. If we wanted to disconnect, then mission accomplished.
    if (mDesiredDevice == null) {
        unadvertiseDisplay();
        // done
        return;
    }
    // Step 5. Try to connect.
    if (mConnectedDevice == null && mConnectingDevice == null) {
        Slog.i(TAG, "Connecting to Wifi display: " + mDesiredDevice.deviceName);
        mConnectingDevice = mDesiredDevice;
        WifiP2pConfig config = new WifiP2pConfig();
        WpsInfo wps = new WpsInfo();
        if (mConnectingDevice.wpsPbcSupported()) {
            wps.setup = WpsInfo.PBC;
        } else if (mConnectingDevice.wpsDisplaySupported()) {
            // We do keypad if peer does display
            wps.setup = WpsInfo.KEYPAD;
        } else {
            wps.setup = WpsInfo.DISPLAY;
        }
        config.wps = wps;
        config.deviceAddress = mConnectingDevice.deviceAddress;
        // Helps with STA & P2P concurrency
        config.groupOwnerIntent = WifiP2pConfig.MIN_GROUP_OWNER_INTENT;
        WifiDisplay display = createWifiDisplay(mConnectingDevice);
        advertiseDisplay(display, null, 0, 0, 0);
        final WifiP2pDevice newDevice = mDesiredDevice;
        mWifiP2pManager.connect(mWifiP2pChannel, config, new ActionListener() {

            @Override
            public void onSuccess() {
                // The connection may not yet be established.  We still need to wait
                // for WIFI_P2P_CONNECTION_CHANGED_ACTION.  However, we might never
                // get that broadcast, so we register a timeout.
                Slog.i(TAG, "Initiated connection to Wifi display: " + newDevice.deviceName);
                mHandler.postDelayed(mConnectionTimeout, CONNECTION_TIMEOUT_SECONDS * 1000);
            }

            @Override
            public void onFailure(int reason) {
                if (mConnectingDevice == newDevice) {
                    Slog.i(TAG, "Failed to initiate connection to Wifi display: " + newDevice.deviceName + ", reason=" + reason);
                    mConnectingDevice = null;
                    handleConnectionFailure(false);
                }
            }
        });
        // wait for asynchronous callback
        return;
    }
    // Step 6. Listen for incoming connections.
    if (mConnectedDevice != null && mRemoteDisplay == null) {
        Inet4Address addr = getInterfaceAddress(mConnectedDeviceGroupInfo);
        if (addr == null) {
            Slog.i(TAG, "Failed to get local interface address for communicating " + "with Wifi display: " + mConnectedDevice.deviceName);
            handleConnectionFailure(false);
            // done
            return;
        }
        setRemoteSubmixOn(true);
        mWifiP2pManager.setMiracastMode(WifiP2pManager.MIRACAST_SOURCE);
        final WifiP2pDevice oldDevice = mConnectedDevice;
        final int port = getPortNumber(mConnectedDevice);
        final String iface = addr.getHostAddress() + ":" + port;
        mRemoteDisplayInterface = iface;
        Slog.i(TAG, "Listening for RTSP connection on " + iface + " from Wifi display: " + mConnectedDevice.deviceName);
        mRemoteDisplay = RemoteDisplay.listen(iface, new RemoteDisplay.Listener() {

            @Override
            public void onDisplayConnected(Surface surface, int width, int height, int flags) {
                if (mConnectedDevice == oldDevice && !mRemoteDisplayConnected) {
                    Slog.i(TAG, "Opened RTSP connection with Wifi display: " + mConnectedDevice.deviceName);
                    mRemoteDisplayConnected = true;
                    mHandler.removeCallbacks(mRtspTimeout);
                    final WifiDisplay display = createWifiDisplay(mConnectedDevice);
                    advertiseDisplay(display, surface, width, height, flags);
                }
            }

            @Override
            public void onDisplayDisconnected() {
                if (mConnectedDevice == oldDevice) {
                    Slog.i(TAG, "Closed RTSP connection with Wifi display: " + mConnectedDevice.deviceName);
                    mHandler.removeCallbacks(mRtspTimeout);
                    disconnect();
                }
            }

            @Override
            public void onDisplayError(int error) {
                if (mConnectedDevice == oldDevice) {
                    Slog.i(TAG, "Lost RTSP connection with Wifi display due to error " + error + ": " + mConnectedDevice.deviceName);
                    mHandler.removeCallbacks(mRtspTimeout);
                    handleConnectionFailure(false);
                }
            }
        }, mHandler);
        mHandler.postDelayed(mRtspTimeout, RTSP_TIMEOUT_SECONDS * 1000);
    }
}
Also used : Inet4Address(java.net.Inet4Address) PeerListListener(android.net.wifi.p2p.WifiP2pManager.PeerListListener) GroupInfoListener(android.net.wifi.p2p.WifiP2pManager.GroupInfoListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) ActionListener(android.net.wifi.p2p.WifiP2pManager.ActionListener) WifiP2pDevice(android.net.wifi.p2p.WifiP2pDevice) WifiP2pConfig(android.net.wifi.p2p.WifiP2pConfig) WifiDisplay(android.hardware.display.WifiDisplay) WpsInfo(android.net.wifi.WpsInfo) Surface(android.view.Surface)

Example 45 with WifiDisplay

use of android.hardware.display.WifiDisplay in project android_frameworks_base by ParanoidAndroid.

the class WifiDisplayController method advertiseDisplay.

private void advertiseDisplay(final WifiDisplay display, final Surface surface, final int width, final int height, final int flags) {
    if (!Objects.equal(mAdvertisedDisplay, display) || mAdvertisedDisplaySurface != surface || mAdvertisedDisplayWidth != width || mAdvertisedDisplayHeight != height || mAdvertisedDisplayFlags != flags) {
        final WifiDisplay oldDisplay = mAdvertisedDisplay;
        final Surface oldSurface = mAdvertisedDisplaySurface;
        mAdvertisedDisplay = display;
        mAdvertisedDisplaySurface = surface;
        mAdvertisedDisplayWidth = width;
        mAdvertisedDisplayHeight = height;
        mAdvertisedDisplayFlags = flags;
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                if (oldSurface != null && surface != oldSurface) {
                    mListener.onDisplayDisconnected();
                } else if (oldDisplay != null && !oldDisplay.hasSameAddress(display)) {
                    mListener.onDisplayConnectionFailed();
                }
                if (display != null) {
                    if (!display.hasSameAddress(oldDisplay)) {
                        mListener.onDisplayConnecting(display);
                    } else if (!display.equals(oldDisplay)) {
                        // The address is the same but some other property such as the
                        // name must have changed.
                        mListener.onDisplayChanged(display);
                    }
                    if (surface != null && surface != oldSurface) {
                        mListener.onDisplayConnected(display, surface, width, height, flags);
                    }
                }
            }
        });
    }
}
Also used : WifiDisplay(android.hardware.display.WifiDisplay) Surface(android.view.Surface)

Aggregations

WifiDisplay (android.hardware.display.WifiDisplay)77 WifiP2pDevice (android.net.wifi.p2p.WifiP2pDevice)12 Surface (android.view.Surface)12 HashMap (java.util.HashMap)10 Map (java.util.Map)10 WpsInfo (android.net.wifi.WpsInfo)6 WifiP2pConfig (android.net.wifi.p2p.WifiP2pConfig)6 ActionListener (android.net.wifi.p2p.WifiP2pManager.ActionListener)6 GroupInfoListener (android.net.wifi.p2p.WifiP2pManager.GroupInfoListener)6 PeerListListener (android.net.wifi.p2p.WifiP2pManager.PeerListListener)6 RemoteException (android.os.RemoteException)6 Inet4Address (java.net.Inet4Address)6 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)6 ArrayList (java.util.ArrayList)5 RemoteDisplay (android.media.RemoteDisplay)3 StackTraceElement (java.lang.StackTraceElement)3 WifiDisplayStatus (android.hardware.display.WifiDisplayStatus)1 MediaRouter (android.media.MediaRouter)1 RouteInfo (android.media.MediaRouter.RouteInfo)1 PreferenceScreen (android.support.v7.preference.PreferenceScreen)1