Chromium Code Reviews| Index: remoting/android/java/src/org/chromium/chromoting/Chromoting.java |
| diff --git a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java |
| index c7338334a87408a18d7ab272525ec0e284b46f8e..ff58ebdb86e71ed45c9c40c3e3b7e8a855bf857f 100644 |
| --- a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java |
| +++ b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java |
| @@ -34,6 +34,10 @@ import org.json.JSONObject; |
| import java.io.IOException; |
| import java.net.URL; |
| import java.net.URLConnection; |
| +import java.util.ArrayList; |
| +import java.util.Collections; |
| +import java.util.Comparator; |
| +import java.util.List; |
| import java.util.Scanner; |
| /** |
| @@ -264,7 +268,7 @@ public class Chromoting extends Activity implements JniInterface.ConnectionListe |
| // Interpret what the directory server told us. |
| JSONObject data = new JSONObject(String.valueOf(response)).getJSONObject("data"); |
| - mHosts = data.getJSONArray("items"); |
| + mHosts = sortHosts(data.getJSONArray("items")); |
| Log.i("hostlist", "Received host listing from directory server"); |
| } catch (RuntimeException ex) { |
| // Make sure any other failure is reported to the user (as an unknown error). |
| @@ -322,6 +326,41 @@ public class Chromoting extends Activity implements JniInterface.ConnectionListe |
| } |
| }); |
| } |
| + |
| + private JSONArray sortHosts(JSONArray hosts) { |
|
Lambros
2014/02/06 00:14:06
nit: This should be static.
I'm in two minds whet
Jamie
2014/02/06 00:47:16
Apparently, non-static inner classes can't have st
|
| + List<JSONObject> hostList = new ArrayList<JSONObject>(); |
| + for (int i = 0; i < hosts.length(); i++) { |
| + try { |
| + hostList.add(hosts.getJSONObject(i)); |
| + } catch (JSONException ex) { |
| + // Ignore non-object entries. |
|
Lambros
2014/02/06 00:14:06
nit: 4-space indent
Jamie
2014/02/06 00:47:16
Done.
|
| + } |
| + } |
| + |
| + Comparator<JSONObject> compareHosts = new Comparator<JSONObject>() { |
| + public int compare(JSONObject a, JSONObject b) { |
| + try { |
| + boolean aOnline = a.getString("status").equals("ONLINE"); |
| + boolean bOnline = b.getString("status").equals("ONLINE"); |
| + if (aOnline && !bOnline) { |
| + return -1; |
|
Lambros
2014/02/06 00:14:06
nit: 4-space indent
Jamie
2014/02/06 00:47:16
Done.
|
| + } else if (bOnline && !aOnline) { |
|
Lambros
2014/02/06 00:14:06
nit: No 'else' after return.
Jamie
2014/02/06 00:47:16
Done.
|
| + return 1; |
|
Lambros
2014/02/06 00:14:06
nit: 4-space indent
Jamie
2014/02/06 00:47:16
Done.
|
| + } |
| + String aName = a.getString("hostName").toUpperCase(); |
| + String bName = b.getString("hostName").toUpperCase(); |
| + return aName.compareTo(bName); |
| + } catch (JSONException ex) { |
| + return 0; |
| + } |
| + } |
| + }; |
| + Collections.sort(hostList, compareHosts); |
| + |
| + JSONArray result = new JSONArray(hostList); |
| + return result; |
| + } |
| + |
|
Lambros
2014/02/06 00:14:06
nit: Remove blank line.
Jamie
2014/02/06 00:47:16
Done.
|
| } |
| /** |