This source file includes following definitions.
- showNoAccountsDialog
- setHostListProgressVisible
- onCreate
- onStart
- onDestroy
- onConfigurationChanged
- onCreateOptionsMenu
- onOptionsItemSelected
- onClick
- connectToHost
- refreshHostList
- run
- onNavigationItemSelected
- onHostListReceived
- onError
- updateUi
- onConnectionState
package org.chromium.chromoting;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.chromium.chromoting.jni.JniInterface;
import java.io.IOException;
import java.util.Arrays;
public class Chromoting extends Activity implements JniInterface.ConnectionListener,
AccountManagerCallback<Bundle>, ActionBar.OnNavigationListener, HostListLoader.Callback,
View.OnClickListener {
private static final String ACCOUNT_TYPE = "com.google";
private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com/auth/chromoting " +
"https://www.googleapis.com/auth/googletalk";
private static final String HELP_URL =
"http://support.google.com/chrome/?p=mobile_crd_hostslist";
private static final String HOST_SETUP_URL =
"https://support.google.com/chrome/answer/1649523";
private Account mAccount;
private Account[] mAccounts;
private AccountsAdapter mAccountsAdapter;
private String mToken;
private HostListLoader mHostListLoader;
private HostInfo[] mHosts = new HostInfo[0];
private MenuItem mRefreshButton;
private ListView mHostListView;
private View mProgressView;
private ProgressDialog mProgressIndicator;
boolean mTriedNewAuthToken;
private void showNoAccountsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.noaccounts_message);
builder.setPositiveButton(R.string.noaccounts_add_account,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_ADD_ACCOUNT);
intent.putExtra(Settings.EXTRA_ACCOUNT_TYPES,
new String[] { ACCOUNT_TYPE });
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
finish();
}
});
builder.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void setHostListProgressVisible(boolean visible) {
mHostListView.setVisibility(visible ? View.GONE : View.VISIBLE);
mProgressView.setVisibility(visible ? View.VISIBLE : View.GONE);
if (visible) {
mHostListView.getEmptyView().setVisibility(View.GONE);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTriedNewAuthToken = false;
mHostListLoader = new HostListLoader();
mHostListView = (ListView)findViewById(R.id.hostList_chooser);
mHostListView.setEmptyView(findViewById(R.id.hostList_empty));
mProgressView = findViewById(R.id.hostList_progress);
findViewById(R.id.host_setup_link_android).setOnClickListener(this);
JniInterface.loadLibrary(this);
}
@Override
public void onStart() {
super.onStart();
mAccounts = AccountManager.get(this).getAccountsByType(ACCOUNT_TYPE);
if (mAccounts.length == 0) {
showNoAccountsDialog();
return;
}
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int index = -1;
if (prefs.contains("account_name") && prefs.contains("account_type")) {
mAccount = new Account(prefs.getString("account_name", null),
prefs.getString("account_type", null));
index = Arrays.asList(mAccounts).indexOf(mAccount);
}
if (index == -1) {
index = 0;
mAccount = mAccounts[0];
}
if (mAccounts.length == 1) {
getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
getActionBar().setTitle(R.string.mode_me2me);
getActionBar().setSubtitle(mAccount.name);
} else {
mAccountsAdapter = new AccountsAdapter(this, mAccounts);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setListNavigationCallbacks(mAccountsAdapter, this);
getActionBar().setSelectedNavigationItem(index);
}
refreshHostList();
}
@Override
public void onDestroy() {
super.onDestroy();
JniInterface.disconnectFromHost();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (mAccounts.length != 1) {
mAccountsAdapter.notifyDataSetChanged();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.chromoting_actionbar, menu);
mRefreshButton = menu.findItem(R.id.actionbar_directoryrefresh);
if (mAccount == null) {
mRefreshButton.setEnabled(false);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionbar_directoryrefresh:
refreshHostList();
return true;
case R.id.actionbar_help:
HelpActivity.launch(this, HELP_URL);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onClick(View view) {
HelpActivity.launch(this, HOST_SETUP_URL);
}
public void connectToHost(HostInfo host) {
mProgressIndicator = ProgressDialog.show(this,
host.name, getString(R.string.footer_connecting), true, true,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
JniInterface.disconnectFromHost();
}
});
SessionConnector connector = new SessionConnector(this, this, mHostListLoader);
connector.connectToHost(mAccount.name, mToken, host);
}
private void refreshHostList() {
mTriedNewAuthToken = false;
setHostListProgressVisible(true);
AccountManager.get(this).getAuthToken(mAccount, TOKEN_SCOPE, null, this, this, null);
}
@Override
public void run(AccountManagerFuture<Bundle> future) {
Log.i("auth", "User finished with auth dialogs");
Bundle result = null;
String explanation = null;
try {
result = future.getResult();
String authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("auth", "Received an auth token from system");
mToken = authToken;
mHostListLoader.retrieveHostList(authToken, this);
} catch (OperationCanceledException ex) {
} catch (AuthenticatorException ex) {
explanation = getString(R.string.error_unexpected);
} catch (IOException ex) {
explanation = getString(R.string.error_network_error);
}
if (result == null) {
if (explanation != null) {
Toast.makeText(this, explanation, Toast.LENGTH_LONG).show();
}
return;
}
String authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("auth", "Received an auth token from system");
mToken = authToken;
mHostListLoader.retrieveHostList(authToken, this);
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
mAccount = mAccounts[itemPosition];
getPreferences(MODE_PRIVATE).edit().putString("account_name", mAccount.name).
putString("account_type", mAccount.type).apply();
mHosts = new HostInfo[0];
updateUi();
refreshHostList();
return true;
}
@Override
public void onHostListReceived(HostInfo[] hosts) {
mHosts = Arrays.copyOf(hosts, hosts.length);
setHostListProgressVisible(false);
updateUi();
}
@Override
public void onError(HostListLoader.Error error) {
String explanation = null;
switch (error) {
case AUTH_FAILED:
break;
case NETWORK_ERROR:
explanation = getString(R.string.error_network_error);
break;
case UNEXPECTED_RESPONSE:
case SERVICE_UNAVAILABLE:
case UNKNOWN:
explanation = getString(R.string.error_unexpected);
break;
default:
return;
}
if (explanation != null) {
Toast.makeText(this, explanation, Toast.LENGTH_LONG).show();
setHostListProgressVisible(false);
return;
}
if (!mTriedNewAuthToken) {
AccountManager authenticator = AccountManager.get(this);
mTriedNewAuthToken = true;
Log.w("auth", "Requesting renewal of rejected auth token");
authenticator.invalidateAuthToken(mAccount.type, mToken);
mToken = null;
authenticator.getAuthToken(mAccount, TOKEN_SCOPE, null, this, this, null);
return;
} else {
Log.e("auth", "Fresh auth token was also rejected");
explanation = getString(R.string.error_authentication_failed);
Toast.makeText(this, explanation, Toast.LENGTH_LONG).show();
setHostListProgressVisible(false);
}
}
private void updateUi() {
mRefreshButton.setEnabled(mAccount != null);
ArrayAdapter<HostInfo> displayer = new HostListAdapter(this, R.layout.host, mHosts);
Log.i("hostlist", "About to populate host list display");
mHostListView.setAdapter(displayer);
}
@Override
public void onConnectionState(JniInterface.ConnectionListener.State state,
JniInterface.ConnectionListener.Error error) {
boolean dismissProgress = false;
switch (state) {
case INITIALIZING:
case CONNECTING:
case AUTHENTICATED:
break;
case CONNECTED:
dismissProgress = true;
startActivityForResult(new Intent(this, Desktop.class), 0);
break;
case FAILED:
dismissProgress = true;
Toast.makeText(this, getString(error.message()), Toast.LENGTH_LONG).show();
finishActivity(0);
break;
case CLOSED:
dismissProgress = true;
finishActivity(0);
break;
default:
assert false : "Unreached";
}
if (dismissProgress && mProgressIndicator != null) {
mProgressIndicator.dismiss();
mProgressIndicator = null;
}
}
}