[Android] DNSSD callback has been modified to be called from the Main Thread. (#33585)

* Fix dnssd callback thread

* Restyled by google-java-format

* comment

* Restyled by google-java-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java b/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java
index 28fc698..e4ac8a6 100644
--- a/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java
+++ b/src/platform/android/java/chip/platform/NsdManagerServiceBrowser.java
@@ -172,7 +172,11 @@
     @Override
     public void onDiscoveryStopped(String serviceType) {
       Log.w(TAG, "Successfully stopped discovery service '" + serviceType);
-      this.handleServiceBrowse(chipMdnsCallback);
+      new Handler(Looper.getMainLooper())
+          .post(
+              () -> {
+                this.handleServiceBrowse(chipMdnsCallback);
+              });
     }
 
     public void handleServiceBrowse(ChipMdnsCallback chipMdnsCallback) {
diff --git a/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java b/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java
index 70ffdbc..c1a1876 100644
--- a/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java
+++ b/src/platform/android/java/chip/platform/NsdServiceFinderAndResolver.java
@@ -21,6 +21,8 @@
 import android.net.nsd.NsdManager;
 import android.net.nsd.NsdServiceInfo;
 import android.net.wifi.WifiManager.MulticastLock;
+import android.os.Handler;
+import android.os.Looper;
 import android.util.Log;
 import androidx.annotation.Nullable;
 import java.util.concurrent.Executors;
@@ -118,20 +120,24 @@
             Log.w(
                 TAG,
                 "Failed to resolve service '" + serviceInfo.getServiceName() + "': " + errorCode);
-            chipMdnsCallback.handleServiceResolve(
-                serviceInfo.getServiceName(),
-                // Use the target service info since the resolved service info sometimes appends a
-                // "." at the front likely because it is trying to strip the service name out of it
-                // and something is missed.
-                // The target service info service type should be effectively the same as the
-                // resolved service info.
-                NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
-                null,
-                null,
-                0,
-                null,
-                callbackHandle,
-                contextHandle);
+            new Handler(Looper.getMainLooper())
+                .post(
+                    () -> {
+                      chipMdnsCallback.handleServiceResolve(
+                          serviceInfo.getServiceName(),
+                          // Use the target service info since the resolved service info sometimes
+                          // appends a "." at the front likely because it is trying to strip the
+                          // service name out of it and something is missed.
+                          // The target service info service type should be effectively the same as
+                          // the resolved service info.
+                          NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
+                          null,
+                          null,
+                          0,
+                          null,
+                          callbackHandle,
+                          contextHandle);
+                    });
 
             if (multicastLock.isHeld()) {
               multicastLock.release();
@@ -153,21 +159,28 @@
                     + serviceInfo.getHost()
                     + ", type : "
                     + serviceInfo.getServiceType());
-            // TODO: Find out if DNS-SD results for Android should contain interface ID
-            chipMdnsCallback.handleServiceResolve(
-                serviceInfo.getServiceName(),
-                // Use the target service info since the resolved service info sometimes appends a
-                // "." at the front likely because it is trying to strip the service name out of it
-                // and something is missed.
-                // The target service info service type should be effectively the same as the
-                // resolved service info.
-                NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
-                serviceInfo.getHost().getHostName(),
-                serviceInfo.getHost().getHostAddress(),
-                serviceInfo.getPort(),
-                serviceInfo.getAttributes(),
-                callbackHandle,
-                contextHandle);
+            final String hostName = serviceInfo.getHost().getHostName();
+            final String address = serviceInfo.getHost().getHostAddress();
+            final int port = serviceInfo.getPort();
+            new Handler(Looper.getMainLooper())
+                .post(
+                    () -> {
+                      // TODO: Find out if DNS-SD results for Android should contain interface ID
+                      chipMdnsCallback.handleServiceResolve(
+                          serviceInfo.getServiceName(),
+                          // Use the target service info since the resolved service info sometimes
+                          // appends a "." at the front likely because it is trying to strip the
+                          // service name out of it and something is missed.
+                          // The target service info service type should be effectively the same as
+                          // the resolved service info.
+                          NsdServiceFinderAndResolver.this.targetServiceInfo.getServiceType(),
+                          hostName,
+                          address,
+                          port,
+                          serviceInfo.getAttributes(),
+                          callbackHandle,
+                          contextHandle);
+                    });
 
             if (multicastLock.isHeld()) {
               multicastLock.release();