OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.accounts.AccountManager; | 8 import android.accounts.AccountManager; |
9 import android.accounts.AccountManagerCallback; | 9 import android.accounts.AccountManagerCallback; |
10 import android.accounts.AccountManagerFuture; | 10 import android.accounts.AccountManagerFuture; |
(...skipping 16 matching lines...) Expand all Loading... | |
27 import android.widget.Toast; | 27 import android.widget.Toast; |
28 | 28 |
29 import org.chromium.chromoting.jni.JniInterface; | 29 import org.chromium.chromoting.jni.JniInterface; |
30 import org.json.JSONArray; | 30 import org.json.JSONArray; |
31 import org.json.JSONException; | 31 import org.json.JSONException; |
32 import org.json.JSONObject; | 32 import org.json.JSONObject; |
33 | 33 |
34 import java.io.IOException; | 34 import java.io.IOException; |
35 import java.net.URL; | 35 import java.net.URL; |
36 import java.net.URLConnection; | 36 import java.net.URLConnection; |
37 import java.util.ArrayList; | |
38 import java.util.Collections; | |
39 import java.util.Comparator; | |
40 import java.util.List; | |
37 import java.util.Scanner; | 41 import java.util.Scanner; |
38 | 42 |
39 /** | 43 /** |
40 * The user interface for querying and displaying a user's host list from the di rectory server. It | 44 * The user interface for querying and displaying a user's host list from the di rectory server. It |
41 * also requests and renews authentication tokens using the system account manag er. | 45 * also requests and renews authentication tokens using the system account manag er. |
42 */ | 46 */ |
43 public class Chromoting extends Activity implements JniInterface.ConnectionListe ner { | 47 public class Chromoting extends Activity implements JniInterface.ConnectionListe ner { |
44 /** Only accounts of this type will be selectable for authentication. */ | 48 /** Only accounts of this type will be selectable for authentication. */ |
45 private static final String ACCOUNT_TYPE = "com.google"; | 49 private static final String ACCOUNT_TYPE = "com.google"; |
46 | 50 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 StringBuilder response = new StringBuilder(); | 261 StringBuilder response = new StringBuilder(); |
258 Scanner incoming = new Scanner(link.getInputStream()); | 262 Scanner incoming = new Scanner(link.getInputStream()); |
259 Log.i("auth", "Successfully authenticated to directory server"); | 263 Log.i("auth", "Successfully authenticated to directory server"); |
260 while (incoming.hasNext()) { | 264 while (incoming.hasNext()) { |
261 response.append(incoming.nextLine()); | 265 response.append(incoming.nextLine()); |
262 } | 266 } |
263 incoming.close(); | 267 incoming.close(); |
264 | 268 |
265 // Interpret what the directory server told us. | 269 // Interpret what the directory server told us. |
266 JSONObject data = new JSONObject(String.valueOf(response)).getJS ONObject("data"); | 270 JSONObject data = new JSONObject(String.valueOf(response)).getJS ONObject("data"); |
267 mHosts = data.getJSONArray("items"); | 271 mHosts = sortHosts(data.getJSONArray("items")); |
268 Log.i("hostlist", "Received host listing from directory server") ; | 272 Log.i("hostlist", "Received host listing from directory server") ; |
269 } catch (RuntimeException ex) { | 273 } catch (RuntimeException ex) { |
270 // Make sure any other failure is reported to the user (as an un known error). | 274 // Make sure any other failure is reported to the user (as an un known error). |
271 throw ex; | 275 throw ex; |
272 } catch (Exception ex) { | 276 } catch (Exception ex) { |
273 // Assemble error message to display to the user. | 277 // Assemble error message to display to the user. |
274 String explanation = getString(R.string.error_unknown); | 278 String explanation = getString(R.string.error_unknown); |
275 if (ex instanceof OperationCanceledException) { | 279 if (ex instanceof OperationCanceledException) { |
276 explanation = getString(R.string.error_auth_canceled); | 280 explanation = getString(R.string.error_auth_canceled); |
277 } else if (ex instanceof AuthenticatorException) { | 281 } else if (ex instanceof AuthenticatorException) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 } | 319 } |
316 | 320 |
317 // Share our findings with the user. | 321 // Share our findings with the user. |
318 runOnUiThread(new Runnable() { | 322 runOnUiThread(new Runnable() { |
319 @Override | 323 @Override |
320 public void run() { | 324 public void run() { |
321 updateUi(); | 325 updateUi(); |
322 } | 326 } |
323 }); | 327 }); |
324 } | 328 } |
329 | |
330 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
| |
331 List<JSONObject> hostList = new ArrayList<JSONObject>(); | |
332 for (int i = 0; i < hosts.length(); i++) { | |
333 try { | |
334 hostList.add(hosts.getJSONObject(i)); | |
335 } catch (JSONException ex) { | |
336 // Ignore non-object entries. | |
Lambros
2014/02/06 00:14:06
nit: 4-space indent
Jamie
2014/02/06 00:47:16
Done.
| |
337 } | |
338 } | |
339 | |
340 Comparator<JSONObject> compareHosts = new Comparator<JSONObject>() { | |
341 public int compare(JSONObject a, JSONObject b) { | |
342 try { | |
343 boolean aOnline = a.getString("status").equals("ONLINE") ; | |
344 boolean bOnline = b.getString("status").equals("ONLINE") ; | |
345 if (aOnline && !bOnline) { | |
346 return -1; | |
Lambros
2014/02/06 00:14:06
nit: 4-space indent
Jamie
2014/02/06 00:47:16
Done.
| |
347 } else if (bOnline && !aOnline) { | |
Lambros
2014/02/06 00:14:06
nit: No 'else' after return.
Jamie
2014/02/06 00:47:16
Done.
| |
348 return 1; | |
Lambros
2014/02/06 00:14:06
nit: 4-space indent
Jamie
2014/02/06 00:47:16
Done.
| |
349 } | |
350 String aName = a.getString("hostName").toUpperCase(); | |
351 String bName = b.getString("hostName").toUpperCase(); | |
352 return aName.compareTo(bName); | |
353 } catch (JSONException ex) { | |
354 return 0; | |
355 } | |
356 } | |
357 }; | |
358 Collections.sort(hostList, compareHosts); | |
359 | |
360 JSONArray result = new JSONArray(hostList); | |
361 return result; | |
362 } | |
363 | |
Lambros
2014/02/06 00:14:06
nit: Remove blank line.
Jamie
2014/02/06 00:47:16
Done.
| |
325 } | 364 } |
326 | 365 |
327 /** | 366 /** |
328 * Updates the infotext and host list display. | 367 * Updates the infotext and host list display. |
329 * This method affects the UI and must be run on the main thread. | 368 * This method affects the UI and must be run on the main thread. |
330 */ | 369 */ |
331 private void updateUi() { | 370 private void updateUi() { |
332 synchronized (mLock) { | 371 synchronized (mLock) { |
333 mRefreshButton.setEnabled(mAccount != null); | 372 mRefreshButton.setEnabled(mAccount != null); |
334 if (mAccount != null) { | 373 if (mAccount != null) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
417 // Unreachable, but required by Google Java style and findbugs. | 456 // Unreachable, but required by Google Java style and findbugs. |
418 assert false : "Unreached"; | 457 assert false : "Unreached"; |
419 } | 458 } |
420 | 459 |
421 if (dismissProgress && mProgressIndicator != null) { | 460 if (dismissProgress && mProgressIndicator != null) { |
422 mProgressIndicator.dismiss(); | 461 mProgressIndicator.dismiss(); |
423 mProgressIndicator = null; | 462 mProgressIndicator = null; |
424 } | 463 } |
425 } | 464 } |
426 } | 465 } |
OLD | NEW |