Kaydet (Commit) 6d6017e3 authored tarafından Artur Dryomov's avatar Artur Dryomov

Add additional comparison for servers in lists.

Sort by class as well as by name. This change should help to show
computers first, then phones and then other devices.

Change-Id: I3a5dec6e5df33b766b70798ac1ad32a5d5db4a3f
üst c856ab73
...@@ -15,6 +15,7 @@ import java.util.Map; ...@@ -15,6 +15,7 @@ import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
...@@ -116,10 +117,26 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, ...@@ -116,10 +117,26 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder,
} }
private Server buildServer(BluetoothDevice aBluetoothDevice) { private Server buildServer(BluetoothDevice aBluetoothDevice) {
Server.Type aServerType = buildServerType(aBluetoothDevice);
String aServerAddress = aBluetoothDevice.getAddress(); String aServerAddress = aBluetoothDevice.getAddress();
String aServerName = aBluetoothDevice.getName(); String aServerName = aBluetoothDevice.getName();
return Server.newBluetoothInstance(aServerAddress, aServerName); return Server.newBluetoothInstance(aServerType, aServerAddress, aServerName);
}
private Server.Type buildServerType(BluetoothDevice aBluetoothDevice) {
int aBluetoothClass = aBluetoothDevice.getBluetoothClass().getMajorDeviceClass();
switch (aBluetoothClass) {
case BluetoothClass.Device.Major.COMPUTER:
return Server.Type.COMPUTER;
case BluetoothClass.Device.Major.PHONE:
return Server.Type.PHONE;
default:
return Server.Type.UNDEFINED;
}
} }
private void callUpdatingServersList() { private void callUpdatingServersList() {
......
...@@ -19,28 +19,38 @@ public class Server implements Parcelable { ...@@ -19,28 +19,38 @@ public class Server implements Parcelable {
TCP, BLUETOOTH TCP, BLUETOOTH
} }
public static enum Type {
COMPUTER, PHONE, UNDEFINED
}
private final Protocol mProtocol; private final Protocol mProtocol;
private final Type mType;
private final String mAddress; private final String mAddress;
private final String mName; private final String mName;
private Server(Protocol aProtocol, String aAddress, String aName) { private Server(Protocol aProtocol, Type aType, String aAddress, String aName) {
this.mProtocol = aProtocol; mProtocol = aProtocol;
this.mAddress = aAddress; mType = aType;
this.mName = aName; mAddress = aAddress;
mName = aName;
} }
public static Server newTcpInstance(String aAddress, String aName) { public static Server newTcpInstance(String aAddress, String aName) {
return new Server(Protocol.TCP, aAddress, aName); return new Server(Protocol.TCP, Type.UNDEFINED, aAddress, aName);
} }
public static Server newBluetoothInstance(String aAddress, String aName) { public static Server newBluetoothInstance(Type aClass, String aAddress, String aName) {
return new Server(Protocol.BLUETOOTH, aAddress, aName); return new Server(Protocol.BLUETOOTH, aClass, aAddress, aName);
} }
public Protocol getProtocol() { public Protocol getProtocol() {
return mProtocol; return mProtocol;
} }
public Type getType() {
return mType;
}
public String getAddress() { public String getAddress() {
return mAddress; return mAddress;
} }
...@@ -64,9 +74,10 @@ public class Server implements Parcelable { ...@@ -64,9 +74,10 @@ public class Server implements Parcelable {
@Override @Override
public void writeToParcel(Parcel aParcel, int aFlags) { public void writeToParcel(Parcel aParcel, int aFlags) {
aParcel.writeString(mProtocol.name());
aParcel.writeString(mType.name());
aParcel.writeString(mAddress); aParcel.writeString(mAddress);
aParcel.writeString(mName); aParcel.writeString(mName);
aParcel.writeString(mProtocol.name());
} }
public static final Parcelable.Creator<Server> CREATOR = new Parcelable.Creator<Server>() { public static final Parcelable.Creator<Server> CREATOR = new Parcelable.Creator<Server>() {
...@@ -80,9 +91,10 @@ public class Server implements Parcelable { ...@@ -80,9 +91,10 @@ public class Server implements Parcelable {
}; };
private Server(Parcel aParcel) { private Server(Parcel aParcel) {
this.mProtocol = Protocol.valueOf(aParcel.readString());
this.mType = Type.valueOf(aParcel.readString());
this.mAddress = aParcel.readString(); this.mAddress = aParcel.readString();
this.mName = aParcel.readString(); this.mName = aParcel.readString();
this.mProtocol = Protocol.valueOf(aParcel.readString());
} }
} }
......
...@@ -22,6 +22,13 @@ import android.content.Context; ...@@ -22,6 +22,13 @@ import android.content.Context;
import org.libreoffice.impressremote.util.Preferences; import org.libreoffice.impressremote.util.Preferences;
class ServersManager implements Comparator<Server> { class ServersManager implements Comparator<Server> {
private static final class CompareResult {
private CompareResult() {
}
public static final int EQUAL = 0;
}
private final ServersFinder mBluetoothServersFinder; private final ServersFinder mBluetoothServersFinder;
private final ServersFinder mTcpServersFinder; private final ServersFinder mTcpServersFinder;
...@@ -94,6 +101,25 @@ class ServersManager implements Comparator<Server> { ...@@ -94,6 +101,25 @@ class ServersManager implements Comparator<Server> {
@Override @Override
public int compare(Server aFirstServer, Server aSecondServer) { public int compare(Server aFirstServer, Server aSecondServer) {
int aServersTypesComparison = compareServersTypes(aFirstServer, aSecondServer);
int aServersNamesComparison = compareServersNames(aFirstServer, aSecondServer);
if (aServersTypesComparison != CompareResult.EQUAL) {
return aServersTypesComparison;
}
else {
return aServersNamesComparison;
}
}
private int compareServersTypes(Server aFirstServer, Server aSecondServer) {
Server.Type aFirstServerType = aFirstServer.getType();
Server.Type aSecondServerType = aSecondServer.getType();
return aFirstServerType.compareTo(aSecondServerType);
}
private int compareServersNames(Server aFirstServer, Server aSecondServer) {
String aFirstServerName = aFirstServer.getName(); String aFirstServerName = aFirstServer.getName();
String aSecondServerName = aSecondServer.getName(); String aSecondServerName = aSecondServer.getName();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment