Skip to content

Commit

Permalink
Fix IPV6 discovery
Browse files Browse the repository at this point in the history
Minor UI changes
  • Loading branch information
albertogeniola committed Nov 5, 2023
1 parent 8184c54 commit b875a71
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
35 changes: 26 additions & 9 deletions app/src/main/res/layout/fragment_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
Expand All @@ -42,25 +43,38 @@

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/httpHostnameInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="56dp"
android:ems="10"
android:hint="HTTP server hostname"
android:hint="API endpoint (http://homeassistant.local:2003)"
android:inputType="textPersonName"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:singleLine="true" />

</com.google.android.material.textfield.TextInputLayout>

<TextView
android:id="@+id/hintForDiscovery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="If unsure, press the discovery button!"
android:gravity="center_horizontal"
android:textColor="@color/design_default_color_secondary_variant"
app:layout_constraintEnd_toEndOf="@+id/httpHostnameInputLayout"
app:layout_constraintStart_toStartOf="@+id/httpHostnameInputLayout"
app:layout_constraintTop_toBottomOf="@+id/httpHostnameInputLayout" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/httpUsernameEditText"
android:layout_width="0dp"
Expand All @@ -74,15 +88,16 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/httpHostnameInputLayout"
app:layout_constraintTop_toBottomOf="@+id/hintForDiscovery"
app:layout_constraintVertical_bias="0.0"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"/>
android:imeOptions="actionNext"
android:singleLine="true" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
Expand Down Expand Up @@ -121,6 +136,7 @@
android:id="@+id/loginButton"
android:drawableEnd="@drawable/ic_baseline_login_24"
android:drawableTint="@color/design_default_color_on_primary"
android:backgroundTint="@color/design_default_color_primary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
Expand All @@ -132,7 +148,8 @@
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/discoveryButton"
app:layout_constraintVertical_bias="0.0" />
app:layout_constraintVertical_bias="0.0"
android:drawableRight="@drawable/ic_baseline_login_24" />

<TextView
android:id="@+id/httpLoginIntroText"
Expand Down

0 comments on commit b875a71

Please sign in to comment.