1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts.common.list;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Color;
22import android.text.TextUtils;
23import android.util.AttributeSet;
24import android.view.Gravity;
25import android.view.View;
26import android.view.ViewParent;
27import android.widget.LinearLayout.LayoutParams;
28import android.widget.TextView;
29
30import com.android.contacts.common.R;
31import com.android.contacts.common.util.ViewUtil;
32
33/**
34 * A custom view for the pinned section header shown at the top of the contact list.
35 */
36public class ContactListPinnedHeaderView extends TextView {
37
38    public ContactListPinnedHeaderView(Context context, AttributeSet attrs, View parent) {
39        super(context, attrs);
40
41        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView);
42        int backgroundColor = a.getColor(
43                R.styleable.ContactListItemView_list_item_background_color, Color.WHITE);
44        int textOffsetTop = a.getDimensionPixelSize(
45                R.styleable.ContactListItemView_list_item_text_offset_top, 0);
46        int paddingStartOffset = a.getDimensionPixelSize(
47                R.styleable.ContactListItemView_list_item_padding_left, 0);
48        int textWidth = getResources().getDimensionPixelSize(
49                R.dimen.contact_list_section_header_width);
50        int widthIncludingPadding = paddingStartOffset + textWidth;
51        a.recycle();
52
53        setBackgroundColor(backgroundColor);
54        setTextAppearance(getContext(), R.style.SectionHeaderStyle);
55        setLayoutParams(new LayoutParams(widthIncludingPadding, LayoutParams.WRAP_CONTENT));
56        setLayoutDirection(parent.getLayoutDirection());
57        setGravity(Gravity.CENTER_VERTICAL |
58                (ViewUtil.isViewLayoutRtl(this) ? Gravity.RIGHT : Gravity.LEFT));
59
60        // Apply text top offset. Multiply by two, because we are implementing this by padding for a
61        // vertically centered view, rather than adjusting the position directly via a layout.
62        setPaddingRelative(
63                getPaddingStart() + paddingStartOffset,
64                getPaddingTop() + (textOffsetTop * 2),
65                getPaddingEnd(),
66                getPaddingBottom());
67    }
68
69    /**
70     * Sets section header or makes it invisible if the title is null.
71     */
72    public void setSectionHeaderTitle(String title) {
73        if (!TextUtils.isEmpty(title)) {
74            setText(title);
75            setVisibility(View.VISIBLE);
76        } else {
77            setVisibility(View.GONE);
78        }
79    }
80}
81