Search in sources :

Example 1 with NsdManager

use of android.net.nsd.NsdManager in project facebook-android-sdk by facebook.

the class DeviceRequestsHelper method cleanUpAdvertisementServiceImpl.

@TargetApi(16)
private static void cleanUpAdvertisementServiceImpl(String userCode) {
    NsdManager.RegistrationListener nsdRegistrationListener = deviceRequestsListeners.get(userCode);
    if (nsdRegistrationListener != null) {
        NsdManager nsdManager = (NsdManager) FacebookSdk.getApplicationContext().getSystemService(Context.NSD_SERVICE);
        nsdManager.unregisterService(nsdRegistrationListener);
        deviceRequestsListeners.remove(userCode);
    }
}
Also used : NsdManager(android.net.nsd.NsdManager) TargetApi(android.annotation.TargetApi)

Example 2 with NsdManager

use of android.net.nsd.NsdManager in project facebook-android-sdk by facebook.

the class DeviceRequestsHelper method startAdvertisementServiceImpl.

@TargetApi(16)
private static boolean startAdvertisementServiceImpl(final String userCode) {
    if (deviceRequestsListeners.containsKey(userCode)) {
        return true;
    }
    // Dots in the version will mess up the Bonjour DNS record parsing
    String sdkVersion = FacebookSdk.getSdkVersion().replace('.', '|');
    // Other SDKs that adopt this feature should use different flavor name
    // The whole name should not exceed 60 characters
    final String nsdServiceName = String.format("%s_%s_%s", // static identifier
    SDK_HEADER, // client app parses the string based on this version
    String.format("%s-%s", SDK_FLAVOR, sdkVersion), // short code for the login flow
    userCode);
    NsdServiceInfo nsdLoginAdvertisementService = new NsdServiceInfo();
    nsdLoginAdvertisementService.setServiceType(SERVICE_TYPE);
    nsdLoginAdvertisementService.setServiceName(nsdServiceName);
    nsdLoginAdvertisementService.setPort(80);
    NsdManager nsdManager = (NsdManager) FacebookSdk.getApplicationContext().getSystemService(Context.NSD_SERVICE);
    NsdManager.RegistrationListener nsdRegistrationListener = new NsdManager.RegistrationListener() {

        @Override
        public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
            // Android may have changed the service name in order to resolve a conflict
            if (!nsdServiceName.equals(NsdServiceInfo.getServiceName())) {
                cleanUpAdvertisementService(userCode);
            }
        }

        @Override
        public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
        }

        @Override
        public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            cleanUpAdvertisementService(userCode);
        }

        @Override
        public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
        }
    };
    deviceRequestsListeners.put(userCode, nsdRegistrationListener);
    nsdManager.registerService(nsdLoginAdvertisementService, NsdManager.PROTOCOL_DNS_SD, nsdRegistrationListener);
    return true;
}
Also used : NsdServiceInfo(android.net.nsd.NsdServiceInfo) NsdManager(android.net.nsd.NsdManager) TargetApi(android.annotation.TargetApi)

Aggregations

TargetApi (android.annotation.TargetApi)2 NsdManager (android.net.nsd.NsdManager)2 NsdServiceInfo (android.net.nsd.NsdServiceInfo)1