From b875a71b708e031b1f263edf3aeecbfff85b18f9 Mon Sep 17 00:00:00 2001 From: Alberto Geniola Date: Sun, 5 Nov 2023 23:48:28 +0100 Subject: [PATCH] Fix IPV6 discovery Minor UI changes --- .../ui/fragments/login/LoginFragment.java | 19 +++++----- .../fragments/pair/ConfigureWifiFragment.java | 6 ++++ app/src/main/res/layout/fragment_login.xml | 35 ++++++++++++++----- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java b/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java index df720b2..193073b 100644 --- a/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java +++ b/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/login/LoginFragment.java @@ -162,7 +162,7 @@ public void onClick(View view) { mRequiresWifiLocation = args.getBoolean(Args.REQUIRES_WIFI_LOCATION, false); mDiscoveryEnabled = args.getBoolean(Args.ENABLE_BROKER_DISCOVERY, false); - mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, "http://homeassistant.local:2002")); + mHttpHostnameEditText.setText(args.getString(Args.HTTP_BROKER_URL, "")); mHttpUsernameEditText.setText(args.getString(Args.HTTP_BROKER_EMAIL, "")); mHttpPasswordEditText.setText(args.getString(Args.HTTP_BROKER_PASSWORD, "")); mDiscoveryButton.setEnabled(mDiscoveryEnabled); @@ -240,16 +240,14 @@ public void run() { public void onResume() { super.onResume(); ((MainActivity)requireActivity()).setWifiLocationWarnRequired(mRequiresWifiLocation); - - if (mDiscoveryEnabled) - startApiDiscovery(); } @Override - public void onPause() { - super.onPause(); + public void onStop() { + super.onStop(); try { - mNsdManager.stopServiceDiscovery(mDiscoveryListener); + if (mDiscoveryEnabled) + mNsdManager.stopServiceDiscovery(mDiscoveryListener); } catch (Exception e) { Log.e(TAG, "Failed to stop service discovery"); } @@ -365,8 +363,13 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { @Override public void onServiceResolved(final NsdServiceInfo serviceInfo) { Log.e(TAG, "Found local API!. " + serviceInfo); - String result = "http://" + serviceInfo.getHost().getHostName()+":"+serviceInfo.getPort(); + // Make sure we only rely on IPv4 Addresses. IPV4 are 4 bytes long. + if (serviceInfo.getHost().getAddress().length!=4) { + Log.w(TAG, "Ignoring discovered IP " + serviceInfo.getHost().getHostAddress() + " as it is not a valid IPv4 address"); + return; + } + String result = "http://" + serviceInfo.getHost().getHostName()+":"+serviceInfo.getPort(); mTimer.cancel(); mTimer = null; if (mDiscoveryInProgress) diff --git a/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/pair/ConfigureWifiFragment.java b/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/pair/ConfigureWifiFragment.java index 53ee186..71ac7d2 100644 --- a/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/pair/ConfigureWifiFragment.java +++ b/app/src/main/java/com/albertogeniola/merossconf/ui/fragments/pair/ConfigureWifiFragment.java @@ -454,6 +454,12 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { public void onServiceResolved(final NsdServiceInfo serviceInfo) { Log.e(TAG, "Resolve Succeeded. " + serviceInfo); mResolveInProgress = false; + // Make sure we only rely on IPv4 Addresses. IPV4 are 4 bytes long. + if (serviceInfo.getHost().getAddress().length!=4) { + Log.w(TAG, "Ignoring discovered IP " + serviceInfo.getHost().getHostAddress() + " as it is not a valid IPv4 address"); + return; + } + configureUi(false, VALIDATE_AND_PROCEED, null); notifyResolveCompleted(serviceInfo.getHost().getHostName(), serviceInfo.getPort()); } diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index bd3dab1..7bb1b8f 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -12,14 +12,15 @@ android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" - android:backgroundTint="@android:color/darker_gray" + android:backgroundTint="@color/design_default_color_secondary_variant" android:text="Discovery" android:drawableTint="@color/design_default_color_on_primary" android:drawableEnd="@drawable/ic_baseline_search_24" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@+id/constraintLayout" /> + app:layout_constraintTop_toBottomOf="@+id/constraintLayout" + android:drawableRight="@drawable/ic_baseline_search_24" /> + app:layout_constraintTop_toTopOf="parent"> + + + + android:imeOptions="actionNext" + android:singleLine="true" /> + + app:layout_constraintVertical_bias="0.0" + android:drawableRight="@drawable/ic_baseline_login_24" />