Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(646)

Unified Diff: remoting/android/java/src/org/chromium/chromoting/Chromoting.java

Issue 148313004: Sort the host list. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewer feedback. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a4b35762fc95ca9a283ca90684ea06eeeb34aa1c 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) {
+ 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.
+ }
+ }
+
+ 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;
+ }
+ if (bOnline && !aOnline) {
+ return 1;
+ }
+ 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;
+ }
}
/**
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698