1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chromoting;
7 import android.text.Html;
8 import android.util.Log;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.ArrayAdapter;
12 import android.widget.TextView;
13 import android.widget.Toast;
15 import org.json.JSONException;
16 import org.json.JSONObject;
18 /** Describes the appearance and behavior of each host list entry. */
19 class HostListAdapter extends ArrayAdapter<JSONObject> {
20 /** Color to use for hosts that are online. */
21 private static final String HOST_COLOR_ONLINE = "green";
23 /** Color to use for hosts that are offline. */
24 private static final String HOST_COLOR_OFFLINE = "red";
26 private Chromoting mChromoting;
29 public HostListAdapter(Chromoting chromoting, int textViewResourceId) {
30 super(chromoting, textViewResourceId);
31 mChromoting = chromoting;
34 /** Generates a View corresponding to this particular host. */
36 public View getView(int position, View convertView, ViewGroup parent) {
37 TextView target = (TextView)super.getView(position, convertView, parent);
40 final JSONObject host = getItem(position);
41 String status = host.getString("status");
42 boolean online = status.equals("ONLINE");
43 target.setText(Html.fromHtml(host.getString("hostName") + " (<font color=\"" +
44 (online ? HOST_COLOR_ONLINE : HOST_COLOR_OFFLINE) + "\">" + status +
48 target.setOnClickListener(new View.OnClickListener() {
50 public void onClick(View v) {
51 mChromoting.connectToHost(host);
55 // Disallow interaction with this entry.
56 target.setEnabled(false);
58 } catch (JSONException ex) {
59 Log.w("hostlist", ex);
60 Toast.makeText(mChromoting, mChromoting.getString(R.string.error_displaying_host),
61 Toast.LENGTH_LONG).show();
63 // Close the application.