From 6d6017e353fe793d571d5d976d27ad0490c6f38a Mon Sep 17 00:00:00 2001 From: Artur Dryomov Date: Tue, 17 Sep 2013 17:33:11 +0300 Subject: 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 --- .../communication/BluetoothServersFinder.java | 19 +++++++++++++- .../impressremote/communication/Server.java | 30 +++++++++++++++------- .../communication/ServersManager.java | 26 +++++++++++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) (limited to 'android') diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java index 4d7619b49e30..29e9b9c1a051 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java @@ -15,6 +15,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; @@ -116,10 +117,26 @@ class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, } private Server buildServer(BluetoothDevice aBluetoothDevice) { + Server.Type aServerType = buildServerType(aBluetoothDevice); String aServerAddress = aBluetoothDevice.getAddress(); 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() { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java index de5c41d47676..4053c4f68680 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java @@ -19,28 +19,38 @@ public class Server implements Parcelable { TCP, BLUETOOTH } + public static enum Type { + COMPUTER, PHONE, UNDEFINED + } + private final Protocol mProtocol; + private final Type mType; private final String mAddress; private final String mName; - private Server(Protocol aProtocol, String aAddress, String aName) { - this.mProtocol = aProtocol; - this.mAddress = aAddress; - this.mName = aName; + private Server(Protocol aProtocol, Type aType, String aAddress, String aName) { + mProtocol = aProtocol; + mType = aType; + mAddress = aAddress; + mName = 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) { - return new Server(Protocol.BLUETOOTH, aAddress, aName); + public static Server newBluetoothInstance(Type aClass, String aAddress, String aName) { + return new Server(Protocol.BLUETOOTH, aClass, aAddress, aName); } public Protocol getProtocol() { return mProtocol; } + public Type getType() { + return mType; + } + public String getAddress() { return mAddress; } @@ -64,9 +74,10 @@ public class Server implements Parcelable { @Override public void writeToParcel(Parcel aParcel, int aFlags) { + aParcel.writeString(mProtocol.name()); + aParcel.writeString(mType.name()); aParcel.writeString(mAddress); aParcel.writeString(mName); - aParcel.writeString(mProtocol.name()); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { @@ -80,9 +91,10 @@ public class Server implements Parcelable { }; private Server(Parcel aParcel) { + this.mProtocol = Protocol.valueOf(aParcel.readString()); + this.mType = Type.valueOf(aParcel.readString()); this.mAddress = aParcel.readString(); this.mName = aParcel.readString(); - this.mProtocol = Protocol.valueOf(aParcel.readString()); } } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java index bb8ac0e99b8c..eac2ec1d9123 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java @@ -22,6 +22,13 @@ import android.content.Context; import org.libreoffice.impressremote.util.Preferences; class ServersManager implements Comparator { + private static final class CompareResult { + private CompareResult() { + } + + public static final int EQUAL = 0; + } + private final ServersFinder mBluetoothServersFinder; private final ServersFinder mTcpServersFinder; @@ -94,6 +101,25 @@ class ServersManager implements Comparator { @Override 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 aSecondServerName = aSecondServer.getName(); -- cgit v1.2.3