summaryrefslogtreecommitdiffstats
path: root/remoting/android
diff options
context:
space:
mode:
authorlambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-03 21:39:56 +0000
committerlambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-03 21:39:56 +0000
commit9891a2da7a9d9fac86daea344d1aace2ae836e54 (patch)
tree0c68acb4ec132e026ddc6c555062b99b4adf7e75 /remoting/android
parenta0e72e989698c94050b10fb154af5b8e6e4c811f (diff)
downloadchromium_src-9891a2da7a9d9fac86daea344d1aace2ae836e54.zip
chromium_src-9891a2da7a9d9fac86daea344d1aace2ae836e54.tar.gz
chromium_src-9891a2da7a9d9fac86daea344d1aace2ae836e54.tar.bz2
Refactor Chromoting.java to pull out some nested classes.
Flattening some of these nested classes will make it simpler to fix the referenced bug. There are also some minor style cleanups. This also adds a private lock, since the previous implementation was synchronizing on the Chromoting object itself, and the newly-refactored code would otherwise trigger a FindBugs warning. The nested class HostListDirectoryGrabber will be addressed in a followup CL. BUG=304719 Review URL: https://codereview.chromium.org/102273006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242931 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/android')
-rw-r--r--remoting/android/java/src/org/chromium/chromoting/Chromoting.java187
-rw-r--r--remoting/android/java/src/org/chromium/chromoting/HostListAdapter.java69
2 files changed, 134 insertions, 122 deletions
diff --git a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java
index cd258d2..520690d 100644
--- a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java
+++ b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java
@@ -11,18 +11,14 @@ import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
-import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
-import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
-import android.view.View;
-import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
@@ -54,11 +50,9 @@ public class Chromoting extends Activity {
private static final String HOST_LIST_PATH =
"https://www.googleapis.com/chromoting/v1/@me/hosts?key=";
- /** Color to use for hosts that are online. */
- private static final String HOST_COLOR_ONLINE = "green";
-
- /** Color to use for hosts that are offline. */
- private static final String HOST_COLOR_OFFLINE = "red";
+ /** Lock to protect |mAccount| and |mToken|. */
+ // TODO(lambroslambrou): |mHosts| needs to be protected as well.
+ private Object mLock = new Object();
/** User's account details. */
private Account mAccount;
@@ -188,12 +182,37 @@ public class Chromoting extends Activity {
return true;
}
+ /** Called when the user taps on a host entry. */
+ public void connectToHost(JSONObject host) {
+ try {
+ synchronized (mLock) {
+ JniInterface.connectToHost(mAccount.name, mToken, host.getString("jabberId"),
+ host.getString("hostId"), host.getString("publicKey"),
+ new Runnable() {
+ @Override
+ public void run() {
+ startActivity(new Intent(Chromoting.this, Desktop.class));
+ }
+ });
+ }
+ } catch (JSONException ex) {
+ Log.w("host", ex);
+ Toast.makeText(this, getString(R.string.error_reading_host),
+ Toast.LENGTH_LONG).show();
+ // Close the application.
+ finish();
+ }
+ }
+
/**
* Processes the authentication token once the system provides it. Once in possession of such a
* token, attempts to request a host list from the directory server. In case of a bad response,
* this is retried once in case the system's cached auth token had expired.
*/
private class HostListDirectoryGrabber implements AccountManagerCallback<Bundle> {
+ // TODO(lambroslambrou): Refactor this class to provide async interface usable on the UI
+ // thread.
+
/** Whether authentication has already been attempted. */
private boolean mAlreadyTried;
@@ -221,7 +240,7 @@ public class Chromoting extends Activity {
String authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("auth", "Received an auth token from system");
- synchronized (mUi) {
+ synchronized (mLock) {
mAccount = new Account(accountName, accountType);
mToken = authToken;
getPreferences(MODE_PRIVATE).edit().putString("account_name", accountName).
@@ -262,7 +281,7 @@ public class Chromoting extends Activity {
if (!mAlreadyTried) {
// This was our first connection attempt.
- synchronized (mUi) {
+ synchronized (mLock) {
if (mAccount != null) {
// We got an account, but couldn't log into it. We'll retry in case
// the system's cached authentication token had already expired.
@@ -289,7 +308,6 @@ public class Chromoting extends Activity {
}
} else if (ex instanceof JSONException) {
explanation = getString(R.string.error_unexpected_response);
- runOnUiThread(new HostListDisplayer(mUi));
}
mHosts = null;
@@ -298,126 +316,51 @@ public class Chromoting extends Activity {
}
// Share our findings with the user.
- runOnUiThread(new HostListDisplayer(mUi));
+ runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ updateUi();
+ }
+ });
}
}
- /** Formats the host list and offers it to the user. */
- private class HostListDisplayer implements Runnable {
- /** Communication with the screen. */
- private Activity mUi;
-
- /** Constructor. */
- public HostListDisplayer(Activity ui) {
- mUi = ui;
- }
-
- /**
- * Updates the infotext and host list display.
- * This method affects the UI and must be run on its same thread.
- */
- @Override
- public void run() {
- synchronized (mUi) {
- mRefreshButton.setEnabled(mAccount != null);
- if (mAccount != null) {
- mAccountSwitcher.setTitle(mAccount.name);
- }
- }
-
- if (mHosts == null) {
- mGreeting.setText(getString(R.string.inst_empty_list));
- mList.setAdapter(null);
- return;
- }
-
- mGreeting.setText(getString(R.string.inst_host_list));
-
- ArrayAdapter<JSONObject> displayer = new HostListAdapter(mUi, R.layout.host);
- Log.i("hostlist", "About to populate host list display");
- try {
- int index = 0;
- while (!mHosts.isNull(index)) {
- displayer.add(mHosts.getJSONObject(index));
- ++index;
- }
- mList.setAdapter(displayer);
- }
- catch(JSONException ex) {
- Log.w("hostlist", ex);
- Toast.makeText(
- mUi, getString(R.string.error_cataloging_hosts), Toast.LENGTH_LONG).show();
-
- // Close the application.
- finish();
+ /**
+ * Updates the infotext and host list display.
+ * This method affects the UI and must be run on the main thread.
+ */
+ private void updateUi() {
+ synchronized (mLock) {
+ mRefreshButton.setEnabled(mAccount != null);
+ if (mAccount != null) {
+ mAccountSwitcher.setTitle(mAccount.name);
}
}
- }
- /** Describes the appearance and behavior of each host list entry. */
- private class HostListAdapter extends ArrayAdapter<JSONObject> {
- /** Constructor. */
- public HostListAdapter(Context context, int textViewResourceId) {
- super(context, textViewResourceId);
+ if (mHosts == null) {
+ mGreeting.setText(getString(R.string.inst_empty_list));
+ mList.setAdapter(null);
+ return;
}
- /** Generates a View corresponding to this particular host. */
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView target = (TextView)super.getView(position, convertView, parent);
+ mGreeting.setText(getString(R.string.inst_host_list));
- try {
- final JSONObject host = getItem(position);
- target.setText(Html.fromHtml(host.getString("hostName") + " (<font color = \"" +
- (host.getString("status").equals("ONLINE") ? HOST_COLOR_ONLINE :
- HOST_COLOR_OFFLINE) + "\">" + host.getString("status") + "</font>)"));
-
- if (host.getString("status").equals("ONLINE")) { // Host is online.
- target.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- synchronized (getContext()) {
- JniInterface.connectToHost(mAccount.name, mToken,
- host.getString("jabberId"),
- host.getString("hostId"),
- host.getString("publicKey"),
- new Runnable() {
- @Override
- public void run() {
- startActivity(
- new Intent(getContext(), Desktop.class));
- }
- });
- }
- }
- catch(JSONException ex) {
- Log.w("host", ex);
- Toast.makeText(getContext(),
- getString(R.string.error_reading_host),
- Toast.LENGTH_LONG).show();
-
- // Close the application.
- finish();
- }
- }
- });
- } else { // Host is offline.
- // Disallow interaction with this entry.
- target.setEnabled(false);
- }
+ ArrayAdapter<JSONObject> displayer = new HostListAdapter(this, R.layout.host);
+ Log.i("hostlist", "About to populate host list display");
+ try {
+ int index = 0;
+ while (!mHosts.isNull(index)) {
+ displayer.add(mHosts.getJSONObject(index));
+ ++index;
}
- catch(JSONException ex) {
- Log.w("hostlist", ex);
- Toast.makeText(getContext(),
- getString(R.string.error_displaying_host),
- Toast.LENGTH_LONG).show();
-
- // Close the application.
- finish();
- }
-
- return target;
+ mList.setAdapter(displayer);
+ } catch (JSONException ex) {
+ Log.w("hostlist", ex);
+ Toast.makeText(this, getString(R.string.error_cataloging_hosts),
+ Toast.LENGTH_LONG).show();
+
+ // Close the application.
+ finish();
}
}
}
diff --git a/remoting/android/java/src/org/chromium/chromoting/HostListAdapter.java b/remoting/android/java/src/org/chromium/chromoting/HostListAdapter.java
new file mode 100644
index 0000000..c4d09e7
--- /dev/null
+++ b/remoting/android/java/src/org/chromium/chromoting/HostListAdapter.java
@@ -0,0 +1,69 @@
+// Copyright 2014 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.text.Html;
+import android.util.Log;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/** Describes the appearance and behavior of each host list entry. */
+class HostListAdapter extends ArrayAdapter<JSONObject> {
+ /** Color to use for hosts that are online. */
+ private static final String HOST_COLOR_ONLINE = "green";
+
+ /** Color to use for hosts that are offline. */
+ private static final String HOST_COLOR_OFFLINE = "red";
+
+ private Chromoting mChromoting;
+
+ /** Constructor. */
+ public HostListAdapter(Chromoting chromoting, int textViewResourceId) {
+ super(chromoting, textViewResourceId);
+ mChromoting = chromoting;
+ }
+
+ /** Generates a View corresponding to this particular host. */
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ TextView target = (TextView)super.getView(position, convertView, parent);
+
+ try {
+ final JSONObject host = getItem(position);
+ String status = host.getString("status");
+ boolean online = status.equals("ONLINE");
+ target.setText(Html.fromHtml(host.getString("hostName") + " (<font color=\"" +
+ (online ? HOST_COLOR_ONLINE : HOST_COLOR_OFFLINE) + "\">" + status +
+ "</font>)"));
+
+ if (online) {
+ target.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ mChromoting.connectToHost(host);
+ }
+ });
+ } else {
+ // Disallow interaction with this entry.
+ target.setEnabled(false);
+ }
+ } catch (JSONException ex) {
+ Log.w("hostlist", ex);
+ Toast.makeText(mChromoting, mChromoting.getString(R.string.error_displaying_host),
+ Toast.LENGTH_LONG).show();
+
+ // Close the application.
+ mChromoting.finish();
+ }
+
+ return target;
+ }
+}