Prevent inserting duplicate entries into child device list
This commit is contained in:
@@ -31,6 +31,8 @@ import android.widget.EditText;
|
|||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
||||||
public class DiscoverActivity extends Activity {
|
public class DiscoverActivity extends Activity {
|
||||||
private static final String PREF_KEY_CHILD_DEVICE_ADDRESS = "childDeviceAddress";
|
private static final String PREF_KEY_CHILD_DEVICE_ADDRESS = "childDeviceAddress";
|
||||||
@@ -189,7 +191,16 @@ public class DiscoverActivity extends Activity {
|
|||||||
public void onServiceResolved(final NsdServiceInfo serviceInfo) {
|
public void onServiceResolved(final NsdServiceInfo serviceInfo) {
|
||||||
Log.i(TAG, "Resolve Succeeded: " + serviceInfo);
|
Log.i(TAG, "Resolve Succeeded: " + serviceInfo);
|
||||||
|
|
||||||
DiscoverActivity.this.runOnUiThread(() -> availableServicesAdapter.add(new ServiceInfoWrapper(serviceInfo)));
|
DiscoverActivity.this.runOnUiThread(() -> {
|
||||||
|
for (int index=0; index < availableServicesAdapter.getCount(); index++) {
|
||||||
|
ServiceInfoWrapper item = availableServicesAdapter.getItem(index);
|
||||||
|
if (item != null && item.matches(serviceInfo)) {
|
||||||
|
// Prevent inserting duplicates
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
availableServicesAdapter.add(new ServiceInfoWrapper(serviceInfo));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -251,6 +262,10 @@ class ServiceInfoWrapper {
|
|||||||
this.info = info;
|
this.info = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean matches(NsdServiceInfo other) {
|
||||||
|
return Objects.equals(this.info.getHost(), other.getHost()) && this.info.getPort() == other.getPort();
|
||||||
|
}
|
||||||
|
|
||||||
public String getAddress()
|
public String getAddress()
|
||||||
{
|
{
|
||||||
return info.getHost().getHostAddress();
|
return info.getHost().getHostAddress();
|
||||||
|
|||||||
Reference in New Issue
Block a user