make BonjourPeer and WifiPeer equals if they point to the same device

This commit is contained in:
Hans-Christoph Steiner 2019-05-23 13:31:36 +02:00
parent 74c3c24f0a
commit 0727787713
2 changed files with 30 additions and 10 deletions

View File

@ -28,15 +28,6 @@ public class BonjourPeer extends WifiPeer {
return serviceInfo.getName(); return serviceInfo.getName();
} }
@Override
public boolean equals(Object peer) {
if (peer instanceof BonjourPeer) {
BonjourPeer that = (BonjourPeer) peer;
return TextUtils.equals(this.getFingerprint(), that.getFingerprint());
}
return false;
}
@Override @Override
public int hashCode() { public int hashCode() {
String fingerprint = getFingerprint(); String fingerprint = getFingerprint();

View File

@ -2,7 +2,7 @@ package org.fdroid.fdroid.localrepo.peers;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
import android.text.TextUtils;
import org.fdroid.fdroid.R; import org.fdroid.fdroid.R;
import org.fdroid.fdroid.data.NewRepoConfig; import org.fdroid.fdroid.data.NewRepoConfig;
@ -26,6 +26,35 @@ public class WifiPeer implements Peer {
this.shouldPromptForSwapBack = shouldPromptForSwapBack; this.shouldPromptForSwapBack = shouldPromptForSwapBack;
} }
/**
* Return if this instance points to the same device as that instance, even
* if some of the configuration details are not the same, like whether one
* instance supplies the fingerprint and the other does not, then use IP
* address and port number.
*/
@Override
public boolean equals(Object peer) {
if (peer instanceof BluetoothPeer) {
return false;
}
String fingerprint = getFingerprint();
if (this instanceof BonjourPeer && peer instanceof BonjourPeer) {
BonjourPeer that = (BonjourPeer) peer;
return TextUtils.equals(this.getFingerprint(), that.getFingerprint());
} else {
WifiPeer that = (WifiPeer) peer;
if (!TextUtils.isEmpty(fingerprint) && TextUtils.equals(this.getFingerprint(), that.getFingerprint())) {
return true;
}
return TextUtils.equals(this.getRepoAddress(), that.getRepoAddress());
}
}
@Override
public int hashCode() {
return (uri.getHost() + uri.getPort()).hashCode();
}
@Override @Override
public String getName() { public String getName() {
return name; return name;