/*
 * Copyright 2014 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.physical_web.physicalweb;

import android.content.Context;
import android.util.Log;
import android.webkit.URLUtil;

import com.youview.tinydnssd.DiscoverResolver;
import com.youview.tinydnssd.MDNSDiscover;

import java.util.Map;


class MdnsUrlDeviceDiscoverer extends UrlDeviceDiscoverer {
  private static final String TAG = "mdns";
  private DiscoverResolver mResolver;
  private static final String MDNS_SERVICE_TYPE = "_physicalweb._tcp";
  private enum State {
    STOPPED,
    WAITING,
    STARTED,
  }
  private State mState;

  public MdnsUrlDeviceDiscoverer(Context context) {
    mState = State.STOPPED;
    resolver = new DiscoverResolver(context, MDNS_SERVICE_TYPE,
        new DiscoverResolver.Listener() {
      @Override
      public void onServicesChanged(Map<String, MDNSDiscover.Result> services) {
        for (MDNSDiscover.Result result : services.values()) {
          // access the Bluetooth MAC from the TXT record
          String url = result.txt.dict.get("url");
          Log.d(TAG, url);
          String id = TAG + result.srv.fqdn + result.srv.port;
          String title = "";
          String description = "";
          if ("false".equals(result.txt.dict.get("public"))) {
            if (result.txt.dict.containsKey("title")) {
              title = result.txt.dict.get("title");
            }
            if (result.txt.dict.containsKey("description")) {
              description = result.txt.dict.get("description");
            }
            reportUrlDevice(createUrlDeviceBuilder(id, url)
              .setPrivate()
              .setTitle(title)
              .setDescription(description)
              .build());
          } else if (URLUtil.isNetworkUrl(url)) {
            reportUrlDevice(createUrlDeviceBuilder(id, url)
              .setPrivate()
              .build());
          }
        }
      }
    });
  }

  @Override
  public synchronized void startScanImpl() {
    if (mState != State.STOPPED) {
      return;
    }
    mState = State.WAITING;
    resolver.start();
  }

  @Override
  public synchronized void stopScanImpl() {
    if (mState != State.STARTED) {
      return;
    }
    mState = State.WAITING;
    resolver.stop();
  }
}

@@ -1238,25 +1238,26 @@
 TOPPED;%0A    
-r
+mR
 esolver = ne
@@ -2622,33 +2622,34 @@
 te.WAITING;%0A    
-r
+mR
 esolver.start();
@@ -2797,17 +2797,18 @@
 NG;%0A    
-r
+mR
 esolver.

Fix mDNS