Bonjour fixes

Change-Id: I1df1dc470bb42c84abc7e1a46bedf9f206910b65
diff --git a/core/java/android/net/nsd/DnsSdServiceInfo.java b/core/java/android/net/nsd/DnsSdServiceInfo.java
index 47d6ec6..33c3eb9 100644
--- a/core/java/android/net/nsd/DnsSdServiceInfo.java
+++ b/core/java/android/net/nsd/DnsSdServiceInfo.java
@@ -19,6 +19,8 @@
 import android.os.Parcelable;
 import android.os.Parcel;
 
+import java.net.InetAddress;
+
 /**
  * Defines a service based on DNS service discovery
  * {@hide}
@@ -27,20 +29,20 @@
 
     private String mServiceName;
 
-    private String mRegistrationType;
+    private String mServiceType;
 
     private DnsSdTxtRecord mTxtRecord;
 
-    private String mHostname;
+    private InetAddress mHost;
 
     private int mPort;
 
-    DnsSdServiceInfo() {
+    public DnsSdServiceInfo() {
     }
 
-    DnsSdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
+    public DnsSdServiceInfo(String sn, String rt, DnsSdTxtRecord tr) {
         mServiceName = sn;
-        mRegistrationType = rt;
+        mServiceType = rt;
         mTxtRecord = tr;
     }
 
@@ -59,13 +61,13 @@
     @Override
     /** @hide */
     public String getServiceType() {
-        return mRegistrationType;
+        return mServiceType;
     }
 
     @Override
     /** @hide */
     public void setServiceType(String s) {
-        mRegistrationType = s;
+        mServiceType = s;
     }
 
     public DnsSdTxtRecord getTxtRecord() {
@@ -76,12 +78,12 @@
         mTxtRecord = new DnsSdTxtRecord(t);
     }
 
-    public String getHostName() {
-        return mHostname;
+    public InetAddress getHost() {
+        return mHost;
     }
 
-    public void setHostName(String s) {
-        mHostname = s;
+    public void setHost(InetAddress s) {
+        mHost = s;
     }
 
     public int getPort() {
@@ -96,7 +98,9 @@
         StringBuffer sb = new StringBuffer();
 
         sb.append("name: ").append(mServiceName).
-            append("type: ").append(mRegistrationType).
+            append("type: ").append(mServiceType).
+            append("host: ").append(mHost).
+            append("port: ").append(mPort).
             append("txtRecord: ").append(mTxtRecord);
         return sb.toString();
     }
@@ -109,9 +113,14 @@
     /** Implement the Parcelable interface */
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeString(mServiceName);
-        dest.writeString(mRegistrationType);
+        dest.writeString(mServiceType);
         dest.writeParcelable(mTxtRecord, flags);
-        dest.writeString(mHostname);
+        if (mHost != null) {
+            dest.writeByte((byte)1);
+            dest.writeByteArray(mHost.getAddress());
+        } else {
+            dest.writeByte((byte)0);
+        }
         dest.writeInt(mPort);
     }
 
@@ -121,9 +130,15 @@
             public DnsSdServiceInfo createFromParcel(Parcel in) {
                 DnsSdServiceInfo info = new DnsSdServiceInfo();
                 info.mServiceName = in.readString();
-                info.mRegistrationType = in.readString();
+                info.mServiceType = in.readString();
                 info.mTxtRecord = in.readParcelable(null);
-                info.mHostname = in.readString();
+
+                if (in.readByte() == 1) {
+                    try {
+                        info.mHost = InetAddress.getByAddress(in.createByteArray());
+                    } catch (java.net.UnknownHostException e) {}
+                }
+
                 info.mPort = in.readInt();
                 return info;
             }
diff --git a/core/java/android/net/nsd/NsdManager.java b/core/java/android/net/nsd/NsdManager.java
index a109a98..505f11b 100644
--- a/core/java/android/net/nsd/NsdManager.java
+++ b/core/java/android/net/nsd/NsdManager.java
@@ -93,6 +93,15 @@
     /** @hide */
     public static final int RESOLVE_SERVICE_SUCCEEDED               = BASE + 17;
 
+    /** @hide */
+    public static final int STOP_RESOLVE                            = BASE + 18;
+    /** @hide */
+    public static final int STOP_RESOLVE_FAILED                     = BASE + 19;
+    /** @hide */
+    public static final int STOP_RESOLVE_SUCCEEDED                  = BASE + 20;
+
+
+
     /**
      * Create a new Nsd instance. Applications use
      * {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve
@@ -117,10 +126,23 @@
 
     /**
      * Indicates that the operation failed because the framework is busy and
-     * unable to service the request
+     * unable to service the request.
      */
     public static final int BUSY                = 2;
 
+    /**
+     * Indicates that the operation failed because it is already active.
+     */
+    public static final int ALREADY_ACTIVE      = 3;
+
+    /**
+     * Indicates that the operation failed because maximum limit on
+     * service registrations has reached.
+     */
+    public static final int MAX_REGS_REACHED    = 4;
+
+
+
     /** Interface for callback invocation when framework channel is connected or lost */
     public interface ChannelListener {
         public void onChannelConnected(Channel c);
@@ -188,6 +210,7 @@
         private DnsSdRegisterListener mDnsSdRegisterListener;
         private DnsSdUpdateRegistrationListener mDnsSdUpdateListener;
         private DnsSdResolveListener mDnsSdResolveListener;
+        private ActionListener mDnsSdStopResolveListener;
 
         AsyncChannel mAsyncChannel;
         ServiceHandler mHandler;
@@ -278,6 +301,16 @@
                                     (DnsSdServiceInfo) message.obj);
                         }
                         break;
+                    case STOP_RESOLVE_FAILED:
+                        if (mDnsSdStopResolveListener!= null) {
+                            mDnsSdStopResolveListener.onFailure(message.arg1);
+                        }
+                        break;
+                    case STOP_RESOLVE_SUCCEEDED:
+                        if (mDnsSdStopResolveListener != null) {
+                            mDnsSdStopResolveListener.onSuccess();
+                        }
+                        break;
                     default:
                         Log.d(TAG, "Ignored " + message);
                         break;
@@ -345,6 +378,14 @@
         c.mDnsSdResolveListener = b;
     }
 
+    /**
+     * Set the listener for stopping service resolution. Can be null.
+     */
+    public void setStopResolveListener(Channel c, ActionListener b) {
+        if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
+        c.mDnsSdStopResolveListener = b;
+    }
+
     public void registerService(Channel c, DnsSdServiceInfo serviceInfo) {
         if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
         if (serviceInfo == null) throw new IllegalArgumentException("Null serviceInfo");
@@ -378,6 +419,13 @@
         c.mAsyncChannel.sendMessage(RESOLVE_SERVICE, serviceInfo);
     }
 
+    public void stopServiceResolve(Channel c) {
+        if (c == null) throw new IllegalArgumentException("Channel needs to be initialized");
+        if (c.mDnsSdResolveListener == null) throw new
+                IllegalStateException("Resolve listener needs to be set first");
+        c.mAsyncChannel.sendMessage(STOP_RESOLVE);
+    }
+
     /**
      * Get a reference to NetworkService handler. This is used to establish
      * an AsyncChannel communication with the service