1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
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.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.chromium.chromoting.jni.JniInterface;
import java.io.IOException;
import java.util.Arrays;
/**
* The user interface for querying and displaying a user's host list from the directory server. It
* also requests and renews authentication tokens using the system account manager.
*/
public class Chromoting extends Activity implements JniInterface.ConnectionListener,
AccountManagerCallback<Bundle>, ActionBar.OnNavigationListener,
HostListLoader.Callback {
/** Only accounts of this type will be selectable for authentication. */
private static final String ACCOUNT_TYPE = "com.google";
/** Scopes at which the authentication token we request will be valid. */
private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com/auth/chromoting " +
"https://www.googleapis.com/auth/googletalk";
/** Web page to be displayed in the Help screen when launched from this activity. */
private static final String HELP_URL =
"http://support.google.com/chrome/?p=mobile_crd_hostslist";
/** User's account details. */
private Account mAccount;
/** List of accounts on the system. */
private Account[] mAccounts;
/** Account auth token. */
private String mToken;
/** Helper for fetching the host list. */
private HostListLoader mHostListLoader;
/** List of hosts. */
private HostInfo[] mHosts;
/** Refresh button. */
private MenuItem mRefreshButton;
/** Greeting at the top of the displayed list. */
private TextView mGreeting;
/** Host list as it appears to the user. */
private ListView mList;
/** Dialog for reporting connection progress. */
private ProgressDialog mProgressIndicator;
/**
* This is set when receiving an authentication error from the HostListLoader. If that occurs,
* this flag is set and a fresh authentication token is fetched from the AccountsService, and
* used to request the host list a second time.
*/
boolean mTriedNewAuthToken;
/** Shows a warning explaining that a Google account is required, then closes the activity. */
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();
}
/**
* Called when the activity is first created. Loads the native library and requests an
* authentication token from the system.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTriedNewAuthToken = false;
mHostListLoader = new HostListLoader();
// Get ahold of our view widgets.
mGreeting = (TextView)findViewById(R.id.hostList_greeting);
mList = (ListView)findViewById(R.id.hostList_chooser);
// Bring native components online.
JniInterface.loadLibrary(this);
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) {
// Preference not loaded, or does not correspond to a valid account, so just pick the
// first account arbitrarily.
index = 0;
mAccount = mAccounts[0];
}
if (mAccounts.length == 1) {
getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setSubtitle(mAccount.name);
} else {
AccountsAdapter adapter = new AccountsAdapter(this, mAccounts);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActionBar().setListNavigationCallbacks(adapter, this);
getActionBar().setSelectedNavigationItem(index);
}
refreshHostList();
}
/** Called when the activity is finally finished. */
@Override
public void onDestroy() {
super.onDestroy();
JniInterface.disconnectFromHost();
}
/** Called when the display is rotated (as registered in the manifest). */
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Reload the spinner resources, since the font sizes are dependent on the screen
// orientation.
if (mAccounts.length != 1) {
AccountsAdapter adapter = new AccountsAdapter(this, mAccounts);
getActionBar().setListNavigationCallbacks(adapter, this);
}
}
/** Called to initialize the action bar. */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.chromoting_actionbar, menu);
mRefreshButton = menu.findItem(R.id.actionbar_directoryrefresh);
if (mAccount == null) {
// If there is no account, don't allow the user to refresh the listing.
mRefreshButton.setEnabled(false);
}
return super.onCreateOptionsMenu(menu);
}
/** Called whenever an action bar button is pressed. */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionbar_directoryrefresh:
refreshHostList();
return true;
case R.id.actionbar_help:
{
Intent intent = new Intent(this, HelpActivity.class);
intent.setData(Uri.parse(HELP_URL));
startActivity(intent);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/** Called when the user taps on a host entry. */
public void connectToHost(HostInfo host) {
SessionConnector connector = new SessionConnector(this, this, mHostListLoader);
connector.connectToHost(mAccount.name, mToken, host);
}
private void refreshHostList() {
mTriedNewAuthToken = false;
// The refresh button simply makes use of the currently-chosen account.
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 {
// Here comes our auth token from the Android system.
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) {
explanation = getString(R.string.error_auth_canceled);
} catch (AuthenticatorException ex) {
explanation = getString(R.string.error_no_accounts);
} catch (IOException ex) {
explanation = getString(R.string.error_bad_connection);
}
if (result == 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();
refreshHostList();
return true;
}
@Override
public void onHostListReceived(HostInfo[] hosts) {
// Store a copy of the array, so that it can't be mutated by the HostListLoader. HostInfo
// is an immutable type, so a shallow copy of the array is sufficient here.
mHosts = Arrays.copyOf(hosts, hosts.length);
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_bad_connection);
break;
case SERVICE_UNAVAILABLE:
case UNEXPECTED_RESPONSE:
explanation = getString(R.string.error_unexpected_response);
break;
case UNKNOWN:
explanation = getString(R.string.error_unknown);
break;
default:
// Unreachable.
return;
}
if (explanation != null) {
Toast.makeText(this, explanation, Toast.LENGTH_LONG).show();
return;
}
// This is the AUTH_FAILED case.
if (!mTriedNewAuthToken) {
// This was our first connection attempt.
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);
// We're not in an error state *yet*.
return;
} else {
// Authentication truly failed.
Log.e("auth", "Fresh auth token was also rejected");
explanation = getString(R.string.error_auth_failed);
Toast.makeText(this, explanation, Toast.LENGTH_LONG).show();
}
}
/**
* Updates the infotext and host list display.
*/
private void updateUi() {
mRefreshButton.setEnabled(mAccount != null);
if (mHosts == null) {
mGreeting.setText(getString(R.string.inst_empty_list));
mList.setAdapter(null);
return;
}
mGreeting.setText(getString(R.string.inst_host_list));
ArrayAdapter<HostInfo> displayer = new HostListAdapter(this, R.layout.host, mHosts);
Log.i("hostlist", "About to populate host list display");
mList.setAdapter(displayer);
}
@Override
public void onConnectionState(JniInterface.ConnectionListener.State state,
JniInterface.ConnectionListener.Error error) {
String stateText = getResources().getStringArray(R.array.protoc_states)[state.value()];
boolean dismissProgress = false;
switch (state) {
case INITIALIZING:
case CONNECTING:
case AUTHENTICATED:
// The connection is still being established, so we'll report the current progress.
if (mProgressIndicator == null) {
mProgressIndicator = ProgressDialog.show(this,
getString(R.string.progress_title), stateText, true, true,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
JniInterface.disconnectFromHost();
}
});
} else {
mProgressIndicator.setMessage(stateText);
}
break;
case CONNECTED:
dismissProgress = true;
Toast.makeText(this, stateText, Toast.LENGTH_SHORT).show();
// Display the remote desktop.
startActivityForResult(new Intent(this, Desktop.class), 0);
break;
case FAILED:
dismissProgress = true;
Toast.makeText(this, stateText + ": "
+ getResources().getStringArray(R.array.protoc_errors)[error.value()],
Toast.LENGTH_LONG).show();
// Close the Desktop view, if it is currently running.
finishActivity(0);
break;
case CLOSED:
// No need to show toast in this case. Either the connection will have failed
// because of an error, which will trigger toast already. Or the disconnection will
// have been initiated by the user.
dismissProgress = true;
finishActivity(0);
break;
default:
// Unreachable, but required by Google Java style and findbugs.
assert false : "Unreached";
}
if (dismissProgress && mProgressIndicator != null) {
mProgressIndicator.dismiss();
mProgressIndicator = null;
}
}
}
|