From be422bfa1bd68dd26673d1c77b554a833acddf02 Mon Sep 17 00:00:00 2001 From: Xcreen Date: Mon, 16 Dec 2024 18:49:34 +0100 Subject: [PATCH 1/3] Fix newInstance deprecation --- app/src/main/java/net/xcreen/restsms/MainActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/net/xcreen/restsms/MainActivity.kt b/app/src/main/java/net/xcreen/restsms/MainActivity.kt index ac8be6c..97b81a6 100644 --- a/app/src/main/java/net/xcreen/restsms/MainActivity.kt +++ b/app/src/main/java/net/xcreen/restsms/MainActivity.kt @@ -54,7 +54,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte //Set Home Fragment val fragmentTransaction = supportFragmentManager.beginTransaction() try { - val homeFragment: Fragment = HomeFragment::class.java.newInstance() + val homeFragment: Fragment = HomeFragment::class.java.getDeclaredConstructor().newInstance() fragmentTransaction.replace(R.id.main_framelayout, homeFragment).commit() } catch (ex: Exception) { ex.printStackTrace() From 6da1e32204937d3184da1b54dce770420b24de73 Mon Sep 17 00:00:00 2001 From: Xcreen Date: Thu, 26 Dec 2024 17:18:18 +0100 Subject: [PATCH 2/3] Added NSD --- .../net/xcreen/restsms/server/SMSServer.kt | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/xcreen/restsms/server/SMSServer.kt b/app/src/main/java/net/xcreen/restsms/server/SMSServer.kt index 557ae20..e4ebf40 100644 --- a/app/src/main/java/net/xcreen/restsms/server/SMSServer.kt +++ b/app/src/main/java/net/xcreen/restsms/server/SMSServer.kt @@ -1,5 +1,10 @@ package net.xcreen.restsms.server +import android.content.Context +import android.net.nsd.NsdManager +import android.net.nsd.NsdServiceInfo +import android.util.Log +import net.xcreen.restsms.AppContext import org.eclipse.jetty.server.Server import org.eclipse.jetty.servlet.ServletContextHandler import org.eclipse.jetty.servlet.ServletHolder @@ -10,7 +15,9 @@ class SMSServer { var goodToken = "" var authEnabled = false private var jettyServer: Server? = null - + private var nsdManager: NsdManager? = null + private var nsdRegistrationListener: NsdManager.RegistrationListener? = null + private val TAG = "SMSServer" /** * Get Server-Logger * @return serverLogging - ServerLogging-Object @@ -38,6 +45,38 @@ class SMSServer { servletContextHandler.addServlet(smsWelcomeServletHolder, "/") jettyServer!!.handler = servletContextHandler + //Start NSD + val nsdServiceInfo = NsdServiceInfo() + nsdServiceInfo.serviceName = "RestSMS" + nsdServiceInfo.serviceType = "_http._tcp." + nsdServiceInfo.port = port + + nsdManager = AppContext.appContext.getSystemService(Context.NSD_SERVICE) as NsdManager + + nsdRegistrationListener = object : NsdManager.RegistrationListener { + override fun onServiceRegistered(serviceInfo: NsdServiceInfo) { + val registeredName = serviceInfo.serviceName + Log.i(TAG, "NSD Service registered: $registeredName") + serverLogging!!.log("info", "NSD Service registered: $registeredName") + } + + override fun onRegistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) { + Log.e(TAG, "NSD Service registration failed: $errorCode") + serverLogging!!.log("error", "NSD Service registration failed: $errorCode") + } + + override fun onServiceUnregistered(serviceInfo: NsdServiceInfo) { + Log.i(TAG, "NSD Service unregistered.") + serverLogging!!.log("info", "NSD Service unregistered.") + } + + override fun onUnregistrationFailed(serviceInfo: NsdServiceInfo, errorCode: Int) { + Log.e(TAG, "NSD Service unregistration failed: $errorCode") + serverLogging!!.log("error", "NSD Service unregistration failed: $errorCode") + } + } + nsdManager!!.registerService(nsdServiceInfo, NsdManager.PROTOCOL_DNS_SD, nsdRegistrationListener) + //Start Jetty jettyServer!!.start() jettyServer!!.join() @@ -53,6 +92,9 @@ class SMSServer { serverLogging!!.log("info", "Stopping Server...") jettyServer!!.stop() } + nsdRegistrationListener?.let { + nsdManager?.unregisterService(it) + } } /** From 2813538601b2f60c3b6d98f536252b820201b22d Mon Sep 17 00:00:00 2001 From: Xcreen Date: Thu, 26 Dec 2024 17:30:32 +0100 Subject: [PATCH 3/3] Added setting to enable/disable nsd --- .../net/xcreen/restsms/fragments/SettingsFragment.kt | 8 ++++++++ .../main/java/net/xcreen/restsms/server/SMSServer.kt | 6 ++++-- .../java/net/xcreen/restsms/server/ServerService.kt | 2 ++ app/src/main/res/layout/fragment_settings.xml | 12 ++++++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/xcreen/restsms/fragments/SettingsFragment.kt b/app/src/main/java/net/xcreen/restsms/fragments/SettingsFragment.kt index d7e9f0e..c7e26b1 100644 --- a/app/src/main/java/net/xcreen/restsms/fragments/SettingsFragment.kt +++ b/app/src/main/java/net/xcreen/restsms/fragments/SettingsFragment.kt @@ -24,6 +24,7 @@ class SettingsFragment : Fragment() { val openBrowserCheckBox = rootView.findViewById(R.id.settings_open_browser_checkbox) val disableLoggingCheckBox = rootView.findViewById(R.id.settings_disable_logging_checkbox) val enableAuth = rootView.findViewById(R.id.settings_enable_auth) + val enableNSD = rootView.findViewById(R.id.settings_enable_nsd) val saveBtn = rootView.findViewById