Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / remoting / android / java / src / org / chromium / chromoting / HostListAdapter.java
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.
4
5 package org.chromium.chromoting;
6
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;
14
15 import org.json.JSONException;
16 import org.json.JSONObject;
17
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";
22
23     /** Color to use for hosts that are offline. */
24     private static final String HOST_COLOR_OFFLINE = "red";
25
26     private Chromoting mChromoting;
27
28     /** Constructor. */
29     public HostListAdapter(Chromoting chromoting, int textViewResourceId) {
30         super(chromoting, textViewResourceId);
31         mChromoting = chromoting;
32     }
33
34     /** Generates a View corresponding to this particular host. */
35     @Override
36     public View getView(int position, View convertView, ViewGroup parent) {
37         TextView target = (TextView)super.getView(position, convertView, parent);
38
39         try {
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 +
45                     "</font>)"));
46
47             if (online) {
48                 target.setOnClickListener(new View.OnClickListener() {
49                         @Override
50                         public void onClick(View v) {
51                             mChromoting.connectToHost(host);
52                         }
53                 });
54             } else {
55                 // Disallow interaction with this entry.
56                 target.setEnabled(false);
57             }
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();
62
63             // Close the application.
64             mChromoting.finish();
65         }
66
67         return target;
68     }
69 }