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.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.app.AlertDialog; | 8 import android.app.AlertDialog; |
| 9 import android.app.ProgressDialog; | 9 import android.app.ProgressDialog; |
| 10 import android.content.DialogInterface; | 10 import android.content.DialogInterface; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 | 71 |
| 72 /** Helper for fetching the host list. */ | 72 /** Helper for fetching the host list. */ |
| 73 private HostListLoader mHostListLoader; | 73 private HostListLoader mHostListLoader; |
| 74 | 74 |
| 75 /** List of hosts. */ | 75 /** List of hosts. */ |
| 76 private HostInfo[] mHosts = new HostInfo[0]; | 76 private HostInfo[] mHosts = new HostInfo[0]; |
| 77 | 77 |
| 78 /** Refresh button. */ | 78 /** Refresh button. */ |
| 79 private MenuItem mRefreshButton; | 79 private MenuItem mRefreshButton; |
| 80 | 80 |
| 81 /** Host list as it appears to the user. */ | 81 /** Host list chooser view shown when at least one host is configured. */ |
| 82 private ListView mHostListView; | 82 private ListView mHostListView; |
| 83 | 83 |
| 84 /** View shown when the user has no configured hosts or host list couldn't b e retrieved. */ | |
| 85 private View mEmptyView; | |
| 86 | |
| 84 /** Progress view shown instead of the host list when the host list is loadi ng. */ | 87 /** Progress view shown instead of the host list when the host list is loadi ng. */ |
| 85 private View mProgressView; | 88 private View mProgressView; |
| 86 | 89 |
| 87 /** Dialog for reporting connection progress. */ | 90 /** Dialog for reporting connection progress. */ |
| 88 private ProgressDialog mProgressIndicator; | 91 private ProgressDialog mProgressIndicator; |
| 89 | 92 |
| 90 /** | 93 /** |
| 91 * Helper used by SessionConnection for session authentication. Receives onN ewIntent() | 94 * Helper used by SessionConnection for session authentication. Receives onN ewIntent() |
| 92 * notifications to handle third-party authentication. | 95 * notifications to handle third-party authentication. |
| 93 */ | 96 */ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 @Override | 144 @Override |
| 142 public void onCancel(DialogInterface dialog) { | 145 public void onCancel(DialogInterface dialog) { |
| 143 finish(); | 146 finish(); |
| 144 } | 147 } |
| 145 }); | 148 }); |
| 146 | 149 |
| 147 AlertDialog dialog = builder.create(); | 150 AlertDialog dialog = builder.create(); |
| 148 dialog.show(); | 151 dialog.show(); |
| 149 } | 152 } |
| 150 | 153 |
| 151 /** Shows or hides the progress indicator for loading the host list. */ | 154 /** |
| 152 private void setHostListProgressVisible(boolean visible) { | 155 * Shows the appropriate view for the host list. If loading is true, shows t he loading |
| 153 mHostListView.setVisibility(visible ? View.GONE : View.VISIBLE); | 156 * indicator. If loading is false, shows either the host list chooser or the host list empty |
| 154 mProgressView.setVisibility(visible ? View.VISIBLE : View.GONE); | 157 * view, depending on whether mHosts contains any hosts. */ |
| 155 | 158 private void switchHostListView(boolean loading) { |
|
Lambros
2016/01/20 19:23:46
This name doesn't indicate what the boolean parame
rkjnsn
2016/01/20 19:39:41
My concern is that that doesn't reflect that it's
| |
| 156 // Hiding the host-list does not automatically hide the empty view, so d o that here. | 159 if (loading) { |
| 157 if (visible) { | 160 mHostListView.setVisibility(View.GONE); |
| 158 mHostListView.getEmptyView().setVisibility(View.GONE); | 161 mEmptyView.setVisibility(View.GONE); |
| 162 mProgressView.setVisibility(View.VISIBLE); | |
| 163 } else { | |
| 164 mHostListView.setVisibility(mHosts.length == 0 ? View.GONE : View.VI SIBLE); | |
| 165 mEmptyView.setVisibility(mHosts.length == 0 ? View.VISIBLE : View.GO NE); | |
| 166 mProgressView.setVisibility(View.GONE); | |
| 159 } | 167 } |
| 160 } | 168 } |
| 161 | 169 |
| 162 /** | 170 /** |
| 163 * Called when the activity is first created. Loads the native library and r equests an | 171 * Called when the activity is first created. Loads the native library and r equests an |
| 164 * authentication token from the system. | 172 * authentication token from the system. |
| 165 */ | 173 */ |
| 166 @Override | 174 @Override |
| 167 public void onCreate(Bundle savedInstanceState) { | 175 public void onCreate(Bundle savedInstanceState) { |
| 168 super.onCreate(savedInstanceState); | 176 super.onCreate(savedInstanceState); |
| 169 setContentView(R.layout.main); | 177 setContentView(R.layout.main); |
| 170 | 178 |
| 171 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | 179 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 172 setSupportActionBar(toolbar); | 180 setSupportActionBar(toolbar); |
| 173 | 181 |
| 174 mTriedNewAuthToken = false; | 182 mTriedNewAuthToken = false; |
| 175 mHostListLoader = new HostListLoader(); | 183 mHostListLoader = new HostListLoader(); |
| 176 | 184 |
| 177 // Get ahold of our view widgets. | 185 // Get ahold of our view widgets. |
| 178 mHostListView = (ListView) findViewById(R.id.hostList_chooser); | 186 mHostListView = (ListView) findViewById(R.id.hostList_chooser); |
| 179 mHostListView.setEmptyView(findViewById(R.id.hostList_empty)); | 187 mEmptyView = findViewById(R.id.hostList_empty); |
| 180 mHostListView.setOnItemClickListener( | 188 mHostListView.setOnItemClickListener( |
| 181 new AdapterView.OnItemClickListener() { | 189 new AdapterView.OnItemClickListener() { |
| 182 @Override | 190 @Override |
| 183 public void onItemClick(AdapterView<?> parent, View view, in t position, | 191 public void onItemClick(AdapterView<?> parent, View view, in t position, |
| 184 long id) { | 192 long id) { |
| 185 onHostClicked(position); | 193 onHostClicked(position); |
| 186 } | 194 } |
| 187 }); | 195 }); |
| 188 | 196 |
| 189 mProgressView = findViewById(R.id.hostList_progress); | 197 mProgressView = findViewById(R.id.hostList_progress); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 mAccountSwitcher.onActivityResult(requestCode, resultCode, data); | 353 mAccountSwitcher.onActivityResult(requestCode, resultCode, data); |
| 346 | 354 |
| 347 if (requestCode == OAuthTokenFetcher.REQUEST_CODE_RECOVER_FROM_OAUTH_ERR OR) { | 355 if (requestCode == OAuthTokenFetcher.REQUEST_CODE_RECOVER_FROM_OAUTH_ERR OR) { |
| 348 if (resultCode == RESULT_OK) { | 356 if (resultCode == RESULT_OK) { |
| 349 // User gave OAuth permission to this app (or recovered from any OAuth failure), | 357 // User gave OAuth permission to this app (or recovered from any OAuth failure), |
| 350 // so retry fetching the token. | 358 // so retry fetching the token. |
| 351 requestAuthToken(false); | 359 requestAuthToken(false); |
| 352 } else { | 360 } else { |
| 353 // User denied permission or cancelled the dialog, so cancel the request. | 361 // User denied permission or cancelled the dialog, so cancel the request. |
| 354 mWaitingForAuthToken = false; | 362 mWaitingForAuthToken = false; |
| 355 setHostListProgressVisible(false); | 363 switchHostListView(false); |
| 356 } | 364 } |
| 357 } | 365 } |
| 358 } | 366 } |
| 359 | 367 |
| 360 /** Called when a permissions request has returned. */ | 368 /** Called when a permissions request has returned. */ |
| 361 @Override | 369 @Override |
| 362 public void onRequestPermissionsResult(int requestCode, String[] permissions , | 370 public void onRequestPermissionsResult(int requestCode, String[] permissions , |
| 363 int[] grantResults) { | 371 int[] grantResults) { |
| 364 // This is currently only used by AccountSwitcherBasic. | 372 // This is currently only used by AccountSwitcherBasic. |
| 365 // Check that the user has granted the needed permission, and reload the accounts. | 373 // Check that the user has granted the needed permission, and reload the accounts. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 446 mAuthenticator = new SessionAuthenticator(this, host); | 454 mAuthenticator = new SessionAuthenticator(this, host); |
| 447 connector.connectToHost(mAccount, mToken, host, mAuthenticator); | 455 connector.connectToHost(mAccount, mToken, host, mAuthenticator); |
| 448 } | 456 } |
| 449 | 457 |
| 450 private void refreshHostList() { | 458 private void refreshHostList() { |
| 451 if (mWaitingForAuthToken) { | 459 if (mWaitingForAuthToken) { |
| 452 return; | 460 return; |
| 453 } | 461 } |
| 454 | 462 |
| 455 mTriedNewAuthToken = false; | 463 mTriedNewAuthToken = false; |
| 456 setHostListProgressVisible(true); | 464 switchHostListView(true); |
| 457 | 465 |
| 458 // The refresh button simply makes use of the currently-chosen account. | 466 // The refresh button simply makes use of the currently-chosen account. |
| 459 requestAuthToken(false); | 467 requestAuthToken(false); |
| 460 } | 468 } |
| 461 | 469 |
| 462 private void requestAuthToken(boolean expireCurrentToken) { | 470 private void requestAuthToken(boolean expireCurrentToken) { |
| 463 mWaitingForAuthToken = true; | 471 mWaitingForAuthToken = true; |
| 464 | 472 |
| 465 OAuthTokenFetcher fetcher = new OAuthTokenFetcher(this, mAccount, | 473 OAuthTokenFetcher fetcher = new OAuthTokenFetcher(this, mAccount, |
| 466 new OAuthTokenFetcher.Callback() { | 474 new OAuthTokenFetcher.Callback() { |
| 467 @Override | 475 @Override |
| 468 public void onTokenFetched(String token) { | 476 public void onTokenFetched(String token) { |
| 469 mWaitingForAuthToken = false; | 477 mWaitingForAuthToken = false; |
| 470 mToken = token; | 478 mToken = token; |
| 471 mHostListLoader.retrieveHostList(mToken, Chromoting.this ); | 479 mHostListLoader.retrieveHostList(mToken, Chromoting.this ); |
| 472 } | 480 } |
| 473 | 481 |
| 474 @Override | 482 @Override |
| 475 public void onError(int errorResource) { | 483 public void onError(int errorResource) { |
| 476 mWaitingForAuthToken = false; | 484 mWaitingForAuthToken = false; |
| 477 setHostListProgressVisible(false); | 485 switchHostListView(false); |
| 478 String explanation = getString(errorResource); | 486 String explanation = getString(errorResource); |
| 479 Toast.makeText(Chromoting.this, explanation, Toast.LENGT H_LONG).show(); | 487 Toast.makeText(Chromoting.this, explanation, Toast.LENGT H_LONG).show(); |
| 480 } | 488 } |
| 481 }); | 489 }); |
| 482 | 490 |
| 483 if (expireCurrentToken) { | 491 if (expireCurrentToken) { |
| 484 fetcher.clearAndFetch(mToken); | 492 fetcher.clearAndFetch(mToken); |
| 485 mToken = null; | 493 mToken = null; |
| 486 } else { | 494 } else { |
| 487 fetcher.fetch(); | 495 fetcher.fetch(); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 506 @Override | 514 @Override |
| 507 public void onRequestCloseDrawer() { | 515 public void onRequestCloseDrawer() { |
| 508 mDrawerLayout.closeDrawers(); | 516 mDrawerLayout.closeDrawers(); |
| 509 } | 517 } |
| 510 | 518 |
| 511 @Override | 519 @Override |
| 512 public void onHostListReceived(HostInfo[] hosts) { | 520 public void onHostListReceived(HostInfo[] hosts) { |
| 513 // Store a copy of the array, so that it can't be mutated by the HostLis tLoader. HostInfo | 521 // Store a copy of the array, so that it can't be mutated by the HostLis tLoader. HostInfo |
| 514 // is an immutable type, so a shallow copy of the array is sufficient he re. | 522 // is an immutable type, so a shallow copy of the array is sufficient he re. |
| 515 mHosts = Arrays.copyOf(hosts, hosts.length); | 523 mHosts = Arrays.copyOf(hosts, hosts.length); |
| 516 setHostListProgressVisible(false); | 524 switchHostListView(false); |
| 517 updateUi(); | 525 updateUi(); |
| 518 } | 526 } |
| 519 | 527 |
| 520 @Override | 528 @Override |
| 521 public void onError(HostListLoader.Error error) { | 529 public void onError(HostListLoader.Error error) { |
| 522 String explanation = null; | 530 String explanation = null; |
| 523 switch (error) { | 531 switch (error) { |
| 524 case AUTH_FAILED: | 532 case AUTH_FAILED: |
| 525 break; | 533 break; |
| 526 case NETWORK_ERROR: | 534 case NETWORK_ERROR: |
| 527 explanation = getString(R.string.error_network_error); | 535 explanation = getString(R.string.error_network_error); |
| 528 break; | 536 break; |
| 529 case UNEXPECTED_RESPONSE: | 537 case UNEXPECTED_RESPONSE: |
| 530 case SERVICE_UNAVAILABLE: | 538 case SERVICE_UNAVAILABLE: |
| 531 case UNKNOWN: | 539 case UNKNOWN: |
| 532 explanation = getString(R.string.error_unexpected); | 540 explanation = getString(R.string.error_unexpected); |
| 533 break; | 541 break; |
| 534 default: | 542 default: |
| 535 // Unreachable. | 543 // Unreachable. |
| 536 return; | 544 return; |
| 537 } | 545 } |
| 538 | 546 |
| 539 if (explanation != null) { | 547 if (explanation != null) { |
| 540 Toast.makeText(this, explanation, Toast.LENGTH_LONG).show(); | 548 Toast.makeText(this, explanation, Toast.LENGTH_LONG).show(); |
| 541 setHostListProgressVisible(false); | 549 switchHostListView(false); |
| 542 return; | 550 return; |
| 543 } | 551 } |
| 544 | 552 |
| 545 // This is the AUTH_FAILED case. | 553 // This is the AUTH_FAILED case. |
| 546 | 554 |
| 547 if (!mTriedNewAuthToken) { | 555 if (!mTriedNewAuthToken) { |
| 548 // This was our first connection attempt. | 556 // This was our first connection attempt. |
| 549 mTriedNewAuthToken = true; | 557 mTriedNewAuthToken = true; |
| 550 requestAuthToken(true); | 558 requestAuthToken(true); |
| 551 | 559 |
| 552 // We're not in an error state *yet*. | 560 // We're not in an error state *yet*. |
| 553 return; | 561 return; |
| 554 } else { | 562 } else { |
| 555 // Authentication truly failed. | 563 // Authentication truly failed. |
| 556 Log.e(TAG, "Fresh auth token was rejected."); | 564 Log.e(TAG, "Fresh auth token was rejected."); |
| 557 explanation = getString(R.string.error_authentication_failed); | 565 explanation = getString(R.string.error_authentication_failed); |
| 558 Toast.makeText(this, explanation, Toast.LENGTH_LONG).show(); | 566 Toast.makeText(this, explanation, Toast.LENGTH_LONG).show(); |
| 559 setHostListProgressVisible(false); | 567 switchHostListView(false); |
| 560 } | 568 } |
| 561 } | 569 } |
| 562 | 570 |
| 563 /** | 571 /** |
| 564 * Updates the infotext and host list display. | 572 * Updates the infotext and host list display. |
| 565 */ | 573 */ |
| 566 private void updateUi() { | 574 private void updateUi() { |
| 567 if (mRefreshButton != null) { | 575 if (mRefreshButton != null) { |
| 568 mRefreshButton.setEnabled(mAccount != null); | 576 mRefreshButton.setEnabled(mAccount != null); |
| 569 } | 577 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 // Unreachable, but required by Google Java style and findbugs. | 614 // Unreachable, but required by Google Java style and findbugs. |
| 607 assert false : "Unreached"; | 615 assert false : "Unreached"; |
| 608 } | 616 } |
| 609 | 617 |
| 610 if (dismissProgress && mProgressIndicator != null) { | 618 if (dismissProgress && mProgressIndicator != null) { |
| 611 mProgressIndicator.dismiss(); | 619 mProgressIndicator.dismiss(); |
| 612 mProgressIndicator = null; | 620 mProgressIndicator = null; |
| 613 } | 621 } |
| 614 } | 622 } |
| 615 } | 623 } |
| OLD | NEW |