| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.os.Handler; | 7 import android.os.Handler; |
| 8 import android.os.HandlerThread; | 8 import android.os.HandlerThread; |
| 9 import android.os.Looper; | 9 import android.os.Looper; |
| 10 import android.util.Log; | 10 import android.util.Log; |
| 11 | 11 |
| 12 import org.chromium.chromoting.jni.JniInterface; | |
| 13 import org.json.JSONArray; | 12 import org.json.JSONArray; |
| 14 import org.json.JSONException; | 13 import org.json.JSONException; |
| 15 import org.json.JSONObject; | 14 import org.json.JSONObject; |
| 16 | 15 |
| 17 import java.io.IOException; | 16 import java.io.IOException; |
| 18 import java.net.HttpURLConnection; | 17 import java.net.HttpURLConnection; |
| 19 import java.net.MalformedURLException; | 18 import java.net.MalformedURLException; |
| 20 import java.net.URL; | 19 import java.net.URL; |
| 21 import java.util.ArrayList; | 20 import java.util.ArrayList; |
| 22 import java.util.Collections; | 21 import java.util.Collections; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 35 } | 34 } |
| 36 | 35 |
| 37 /** Callback for receiving the host list, or getting notified of an error. *
/ | 36 /** Callback for receiving the host list, or getting notified of an error. *
/ |
| 38 public interface Callback { | 37 public interface Callback { |
| 39 void onHostListReceived(HostInfo[] hosts); | 38 void onHostListReceived(HostInfo[] hosts); |
| 40 void onError(Error error); | 39 void onError(Error error); |
| 41 } | 40 } |
| 42 | 41 |
| 43 /** Path from which to download a user's host list JSON object. */ | 42 /** Path from which to download a user's host list JSON object. */ |
| 44 private static final String HOST_LIST_PATH = | 43 private static final String HOST_LIST_PATH = |
| 45 "https://www.googleapis.com/chromoting/v1/@me/hosts?key="; | 44 "https://www.googleapis.com/chromoting/v1/@me/hosts"; |
| 46 | 45 |
| 47 /** Callback handler to be used for network operations. */ | 46 /** Callback handler to be used for network operations. */ |
| 48 private Handler mNetworkThread; | 47 private Handler mNetworkThread; |
| 49 | 48 |
| 50 /** Handler for main thread. */ | 49 /** Handler for main thread. */ |
| 51 private Handler mMainThread; | 50 private Handler mMainThread; |
| 52 | 51 |
| 53 public HostListLoader() { | 52 public HostListLoader() { |
| 54 // Thread responsible for downloading the host list. | 53 // Thread responsible for downloading the host list. |
| 55 | 54 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 79 public void run() { | 78 public void run() { |
| 80 doRetrieveHostList(authTokenFinal, callbackFinal); | 79 doRetrieveHostList(authTokenFinal, callbackFinal); |
| 81 } | 80 } |
| 82 }); | 81 }); |
| 83 } | 82 } |
| 84 | 83 |
| 85 private void doRetrieveHostList(String authToken, Callback callback) { | 84 private void doRetrieveHostList(String authToken, Callback callback) { |
| 86 HttpURLConnection link = null; | 85 HttpURLConnection link = null; |
| 87 String response = null; | 86 String response = null; |
| 88 try { | 87 try { |
| 89 link = (HttpURLConnection) | 88 link = (HttpURLConnection) new URL(HOST_LIST_PATH).openConnection(); |
| 90 new URL(HOST_LIST_PATH + JniInterface.nativeGetApiKey()).ope
nConnection(); | |
| 91 link.addRequestProperty("client_id", JniInterface.nativeGetClientId(
)); | |
| 92 link.addRequestProperty("client_secret", JniInterface.nativeGetClien
tSecret()); | |
| 93 link.setRequestProperty("Authorization", "OAuth " + authToken); | 89 link.setRequestProperty("Authorization", "OAuth " + authToken); |
| 94 | 90 |
| 95 // Listen for the server to respond. | 91 // Listen for the server to respond. |
| 96 int status = link.getResponseCode(); | 92 int status = link.getResponseCode(); |
| 97 switch (status) { | 93 switch (status) { |
| 98 case HttpURLConnection.HTTP_OK: // 200 | 94 case HttpURLConnection.HTTP_OK: // 200 |
| 99 break; | 95 break; |
| 100 case HttpURLConnection.HTTP_UNAUTHORIZED: // 401 | 96 case HttpURLConnection.HTTP_UNAUTHORIZED: // 401 |
| 101 postError(callback, Error.AUTH_FAILED); | 97 postError(callback, Error.AUTH_FAILED); |
| 102 return; | 98 return; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 return a.isOnline ? -1 : 1; | 184 return a.isOnline ? -1 : 1; |
| 189 } | 185 } |
| 190 String aName = a.name.toUpperCase(Locale.getDefault()); | 186 String aName = a.name.toUpperCase(Locale.getDefault()); |
| 191 String bName = b.name.toUpperCase(Locale.getDefault()); | 187 String bName = b.name.toUpperCase(Locale.getDefault()); |
| 192 return aName.compareTo(bName); | 188 return aName.compareTo(bName); |
| 193 } | 189 } |
| 194 }; | 190 }; |
| 195 Collections.sort(hosts, hostComparator); | 191 Collections.sort(hosts, hostComparator); |
| 196 } | 192 } |
| 197 } | 193 } |
| OLD | NEW |