Chromium Code Reviews| 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; |
| 11 import android.accounts.AuthenticatorException; | 11 import android.accounts.AuthenticatorException; |
| 12 import android.accounts.OperationCanceledException; | 12 import android.accounts.OperationCanceledException; |
| 13 import android.app.Activity; | 13 import android.app.Activity; |
| 14 import android.app.ProgressDialog; | |
| 15 import android.content.DialogInterface; | |
| 14 import android.content.Intent; | 16 import android.content.Intent; |
| 15 import android.content.SharedPreferences; | 17 import android.content.SharedPreferences; |
| 16 import android.os.Bundle; | 18 import android.os.Bundle; |
| 17 import android.os.Handler; | 19 import android.os.Handler; |
| 18 import android.os.HandlerThread; | 20 import android.os.HandlerThread; |
| 19 import android.util.Log; | 21 import android.util.Log; |
| 20 import android.view.Menu; | 22 import android.view.Menu; |
| 21 import android.view.MenuItem; | 23 import android.view.MenuItem; |
| 22 import android.widget.ArrayAdapter; | 24 import android.widget.ArrayAdapter; |
| 23 import android.widget.ListView; | 25 import android.widget.ListView; |
| 24 import android.widget.TextView; | 26 import android.widget.TextView; |
| 25 import android.widget.Toast; | 27 import android.widget.Toast; |
| 26 | 28 |
| 27 import org.chromium.chromoting.jni.JniInterface; | 29 import org.chromium.chromoting.jni.JniInterface; |
| 28 import org.json.JSONArray; | 30 import org.json.JSONArray; |
| 29 import org.json.JSONException; | 31 import org.json.JSONException; |
| 30 import org.json.JSONObject; | 32 import org.json.JSONObject; |
| 31 | 33 |
| 32 import java.io.IOException; | 34 import java.io.IOException; |
| 33 import java.net.URL; | 35 import java.net.URL; |
| 34 import java.net.URLConnection; | 36 import java.net.URLConnection; |
| 35 import java.util.Scanner; | 37 import java.util.Scanner; |
| 36 | 38 |
| 37 /** | 39 /** |
| 38 * The user interface for querying and displaying a user's host list from the di rectory server. It | 40 * The user interface for querying and displaying a user's host list from the di rectory server. It |
| 39 * also requests and renews authentication tokens using the system account manag er. | 41 * also requests and renews authentication tokens using the system account manag er. |
| 40 */ | 42 */ |
| 41 public class Chromoting extends Activity { | 43 public class Chromoting extends Activity implements JniInterface.ConnectionListe ner { |
| 42 /** Only accounts of this type will be selectable for authentication. */ | 44 /** Only accounts of this type will be selectable for authentication. */ |
| 43 private static final String ACCOUNT_TYPE = "com.google"; | 45 private static final String ACCOUNT_TYPE = "com.google"; |
| 44 | 46 |
| 45 /** Scopes at which the authentication token we request will be valid. */ | 47 /** Scopes at which the authentication token we request will be valid. */ |
| 46 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com /auth/chromoting " + | 48 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com /auth/chromoting " + |
| 47 "https://www.googleapis.com/auth/googletalk"; | 49 "https://www.googleapis.com/auth/googletalk"; |
| 48 | 50 |
| 49 /** Path from which to download a user's host list JSON object. */ | 51 /** Path from which to download a user's host list JSON object. */ |
| 50 private static final String HOST_LIST_PATH = | 52 private static final String HOST_LIST_PATH = |
| 51 "https://www.googleapis.com/chromoting/v1/@me/hosts?key="; | 53 "https://www.googleapis.com/chromoting/v1/@me/hosts?key="; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 71 | 73 |
| 72 /** Greeting at the top of the displayed list. */ | 74 /** Greeting at the top of the displayed list. */ |
| 73 private TextView mGreeting; | 75 private TextView mGreeting; |
| 74 | 76 |
| 75 /** Host list as it appears to the user. */ | 77 /** Host list as it appears to the user. */ |
| 76 private ListView mList; | 78 private ListView mList; |
| 77 | 79 |
| 78 /** Callback handler to be used for network operations. */ | 80 /** Callback handler to be used for network operations. */ |
| 79 private Handler mNetwork; | 81 private Handler mNetwork; |
| 80 | 82 |
| 83 /** Dialog for reporting connection progress. */ | |
| 84 private ProgressDialog mProgressIndicator; | |
| 85 | |
| 81 /** | 86 /** |
| 82 * Called when the activity is first created. Loads the native library and r equests an | 87 * Called when the activity is first created. Loads the native library and r equests an |
| 83 * authentication token from the system. | 88 * authentication token from the system. |
| 84 */ | 89 */ |
| 85 @Override | 90 @Override |
| 86 public void onCreate(Bundle savedInstanceState) { | 91 public void onCreate(Bundle savedInstanceState) { |
| 87 super.onCreate(savedInstanceState); | 92 super.onCreate(savedInstanceState); |
| 88 setContentView(R.layout.main); | 93 setContentView(R.layout.main); |
| 89 | 94 |
| 90 // Get ahold of our view widgets. | 95 // Get ahold of our view widgets. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 } | 185 } |
| 181 | 186 |
| 182 return true; | 187 return true; |
| 183 } | 188 } |
| 184 | 189 |
| 185 /** Called when the user taps on a host entry. */ | 190 /** Called when the user taps on a host entry. */ |
| 186 public void connectToHost(JSONObject host) { | 191 public void connectToHost(JSONObject host) { |
| 187 try { | 192 try { |
| 188 synchronized (mLock) { | 193 synchronized (mLock) { |
| 189 JniInterface.connectToHost(mAccount.name, mToken, host.getString ("jabberId"), | 194 JniInterface.connectToHost(mAccount.name, mToken, host.getString ("jabberId"), |
| 190 host.getString("hostId"), host.getString("publicKey"), | 195 host.getString("hostId"), host.getString("publicKey"), t his); |
| 191 new Runnable() { | |
| 192 @Override | |
| 193 public void run() { | |
| 194 startActivity(new Intent(Chromoting.this, De sktop.class)); | |
| 195 } | |
| 196 }); | |
| 197 } | 196 } |
| 198 } catch (JSONException ex) { | 197 } catch (JSONException ex) { |
| 199 Log.w("host", ex); | 198 Log.w("host", ex); |
| 200 Toast.makeText(this, getString(R.string.error_reading_host), | 199 Toast.makeText(this, getString(R.string.error_reading_host), |
| 201 Toast.LENGTH_LONG).show(); | 200 Toast.LENGTH_LONG).show(); |
| 202 // Close the application. | 201 // Close the application. |
| 203 finish(); | 202 finish(); |
| 204 } | 203 } |
| 205 } | 204 } |
| 206 | 205 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 mList.setAdapter(displayer); | 352 mList.setAdapter(displayer); |
| 354 } catch (JSONException ex) { | 353 } catch (JSONException ex) { |
| 355 Log.w("hostlist", ex); | 354 Log.w("hostlist", ex); |
| 356 Toast.makeText(this, getString(R.string.error_cataloging_hosts), | 355 Toast.makeText(this, getString(R.string.error_cataloging_hosts), |
| 357 Toast.LENGTH_LONG).show(); | 356 Toast.LENGTH_LONG).show(); |
| 358 | 357 |
| 359 // Close the application. | 358 // Close the application. |
| 360 finish(); | 359 finish(); |
| 361 } | 360 } |
| 362 } | 361 } |
| 362 | |
| 363 @Override | |
| 364 public void onConnectionState(int state, int error) { | |
| 365 if (state < SUCCESSFUL_CONNECTION && error == 0) { | |
|
Sergey Ulanov
2014/01/03 00:01:51
error is meaningful only when state==FAILED, so ch
Lambros
2014/01/04 01:59:46
Done.
| |
| 366 // The connection is still being established, so we'll report the cu rrent progress. | |
| 367 if (mProgressIndicator == null) { | |
| 368 mProgressIndicator = ProgressDialog.show(this, getString(R.strin g.progress_title), | |
| 369 getResources().getStringArray(R.array.protoc_states)[sta te], true, true, | |
| 370 new DialogInterface.OnCancelListener() { | |
| 371 @Override | |
| 372 public void onCancel(DialogInterface dialog) { | |
| 373 JniInterface.disconnectFromHost(); | |
| 374 } | |
| 375 }); | |
| 376 } else { | |
| 377 mProgressIndicator.setMessage( | |
| 378 getResources().getStringArray(R.array.protoc_states) [state]); | |
| 379 } | |
| 380 } else { | |
| 381 // The connection is complete or has failed, so we can lose the prog ress indicator. | |
| 382 if (mProgressIndicator != null) { | |
| 383 mProgressIndicator.dismiss(); | |
| 384 mProgressIndicator = null; | |
| 385 } | |
| 386 | |
| 387 if (state == SUCCESSFUL_CONNECTION) { | |
| 388 Toast.makeText(this, getResources().getStringArray(R.array.proto c_states)[state], | |
| 389 Toast.LENGTH_SHORT).show(); | |
| 390 | |
| 391 // Display the remote desktop. | |
| 392 startActivityForResult(new Intent(this, Desktop.class), 0); | |
| 393 } else { | |
| 394 Toast.makeText(this, getResources().getStringArray(R.array.proto c_states)[state] | |
| 395 + (error == 0 ? "" : ": " | |
|
Sergey Ulanov
2014/01/03 00:01:51
no need to check error==0 here. error shouldn't be
Lambros
2014/01/04 01:59:46
Done.
| |
| 396 + getResources().getStringArray(R.array.protoc_errors)[e rror]), | |
| 397 Toast.LENGTH_LONG).show(); | |
| 398 | |
| 399 // Close the Desktop view, if it is currently running. | |
| 400 finishActivity(0); | |
| 401 } | |
| 402 } | |
| 403 } | |
| 363 } | 404 } |
| OLD | NEW |