Chromium Code Reviews| Index: remoting/android/java/src/org/chromium/chromoting/Chromoting.java |
| diff --git a/remoting/android/java/src/org/chromium/chromoting/Chromoting.java b/remoting/android/java/src/org/chromium/chromoting/Chromoting.java |
| index cd258d2532bb4fafa6be1cd6545eba976d1e6725..264d8a2eab9e55ecc6636b4da0897c5ddec2d0aa 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(); |
|
Sergey Ulanov
2013/12/28 02:17:30
Do we really need this lock? It won't be necessary
Lambros
2013/12/30 21:46:35
We still need the network thread. I don't know if
|
| /** User's account details. */ |
| private Account mAccount; |
| @@ -188,6 +182,28 @@ 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, |
| @@ -221,7 +237,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 +278,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 +305,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 +313,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(); |
| } |
| } |
| } |