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
5package org.chromium.chromoting;
6
7import android.view.View;
8import android.view.ViewGroup;
9import android.widget.ArrayAdapter;
10import android.widget.TextView;
11import android.widget.Toast;
12
13/** Describes the appearance and behavior of each host list entry. */
14class HostListAdapter extends ArrayAdapter<HostInfo> {
15    /** Color to use for hosts that are online. */
16    private static final String HOST_COLOR_ONLINE = "green";
17
18    /** Color to use for hosts that are offline. */
19    private static final String HOST_COLOR_OFFLINE = "red";
20
21    private Chromoting mChromoting;
22
23    /** Constructor. */
24    public HostListAdapter(Chromoting chromoting, int textViewResourceId, HostInfo[] hosts) {
25        super(chromoting, textViewResourceId, hosts);
26        mChromoting = chromoting;
27    }
28
29    /** Generates a View corresponding to this particular host. */
30    @Override
31    public View getView(int position, View convertView, ViewGroup parent) {
32        TextView target = (TextView) super.getView(position, convertView, parent);
33
34        final HostInfo host = getItem(position);
35
36        target.setText(host.name);
37        target.setCompoundDrawablesWithIntrinsicBounds(
38                host.isOnline ? R.drawable.icon_host : R.drawable.icon_host_offline, 0, 0, 0);
39
40        if (host.isOnline) {
41            target.setOnClickListener(new View.OnClickListener() {
42                    @Override
43                    public void onClick(View v) {
44                        mChromoting.connectToHost(host);
45                    }
46            });
47        } else {
48            target.setTextColor(mChromoting.getResources().getColor(R.color.host_offline_text));
49            target.setBackgroundResource(R.drawable.list_item_disabled_selector);
50            target.setOnClickListener(new View.OnClickListener() {
51                    @Override
52                    public void onClick(View v) {
53                        Toast.makeText(mChromoting,
54                                mChromoting.getString(R.string.host_offline_tooltip),
55                                Toast.LENGTH_SHORT).show();
56                    }
57            });
58        }
59
60        return target;
61    }
62}
63