1// Copyright 2013 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.ui.autofill;
6
7import android.content.Context;
8import android.graphics.Color;
9import android.text.TextUtils;
10import android.view.LayoutInflater;
11import android.view.View;
12import android.view.ViewGroup;
13import android.widget.AbsListView.LayoutParams;
14import android.widget.ArrayAdapter;
15import android.widget.TextView;
16
17import org.chromium.base.ApiCompatibilityUtils;
18import org.chromium.ui.R;
19
20import java.util.ArrayList;
21import java.util.Set;
22
23/**
24 * Autofill suggestion adapter for AutofillWindow.
25 */
26public class AutofillListAdapter extends ArrayAdapter<AutofillSuggestion> {
27    private Context mContext;
28    private Set<Integer> mSeparators;
29
30    AutofillListAdapter(Context context,
31                        ArrayList<AutofillSuggestion> objects,
32                        Set<Integer> separators) {
33        super(context, R.layout.autofill_text, objects);
34        mSeparators = separators;
35        mContext = context;
36    }
37
38    @Override
39    public View getView(int position, View convertView, ViewGroup parent) {
40        View layout = convertView;
41        if (convertView == null) {
42            LayoutInflater inflater =
43                    (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
44            layout = inflater.inflate(R.layout.autofill_text, null);
45            ApiCompatibilityUtils.setBackgroundForView(layout, new AutofillDividerDrawable());
46        }
47        TextView labelView = (TextView) layout.findViewById(R.id.autofill_label);
48        labelView.setText(getItem(position).mLabel);
49
50        AutofillDividerDrawable divider = (AutofillDividerDrawable) layout.getBackground();
51        int height = mContext.getResources().getDimensionPixelSize(R.dimen.autofill_text_height);
52        if (position == 0) {
53            divider.setColor(Color.TRANSPARENT);
54        } else {
55            int dividerHeight = mContext.getResources().getDimensionPixelSize(
56                    R.dimen.autofill_text_divider_height);
57            height += dividerHeight;
58            divider.setHeight(dividerHeight);
59            if (mSeparators.contains(position)) {
60                divider.setColor(mContext.getResources().getColor(
61                                 R.color.autofill_dark_divider_color));
62            } else {
63                divider.setColor(mContext.getResources().getColor(
64                                 R.color.autofill_divider_color));
65            }
66        }
67        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, height));
68
69        TextView sublabelView = (TextView) layout.findViewById(R.id.autofill_sublabel);
70        CharSequence sublabel = getItem(position).mSublabel;
71        if (TextUtils.isEmpty(sublabel)) {
72            sublabelView.setVisibility(View.GONE);
73        } else {
74            sublabelView.setText(sublabel);
75            sublabelView.setVisibility(View.VISIBLE);
76        }
77
78        return layout;
79    }
80}
81