1d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng/*
2d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * Copyright (C) 2010 The Android Open Source Project
3d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng *
4d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
5d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * you may not use this file except in compliance with the License.
6d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * You may obtain a copy of the License at
7d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng *
8d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
9d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng *
10d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * Unless required by applicable law or agreed to in writing, software
11d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
12d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * See the License for the specific language governing permissions and
14d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * limitations under the License.
15d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng */
16d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
17d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengpackage com.android.contacts.common.list;
18d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
19d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.content.Context;
20d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.content.res.TypedArray;
21d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.graphics.Color;
22d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.text.TextUtils;
23d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.util.AttributeSet;
24d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.view.Gravity;
25d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.view.View;
2653c7dc1e9691b5b427395642a4274d37a1a15bb4Yorke Leeimport android.view.ViewParent;
271f1e90d36fe914a7ce51e6f9884199580115d891Andrew Leeimport android.widget.LinearLayout.LayoutParams;
28d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport android.widget.TextView;
29d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
30d6bba124836ec2f528b329759e38fda6297fec49Chiao Chengimport com.android.contacts.common.R;
31ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Leeimport com.android.contacts.common.util.ViewUtil;
32d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
33d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng/**
34d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng * A custom view for the pinned section header shown at the top of the contact list.
35d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng */
361f1e90d36fe914a7ce51e6f9884199580115d891Andrew Leepublic class ContactListPinnedHeaderView extends TextView {
37d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
3853c7dc1e9691b5b427395642a4274d37a1a15bb4Yorke Lee    public ContactListPinnedHeaderView(Context context, AttributeSet attrs, View parent) {
39d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        super(context, attrs);
40d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
4125527b9fe191063927e211bcf86d5cd26ab6b67aYorke Lee        if (R.styleable.ContactListItemView == null) {
4225527b9fe191063927e211bcf86d5cd26ab6b67aYorke Lee            return;
4325527b9fe191063927e211bcf86d5cd26ab6b67aYorke Lee        }
44d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView);
451f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee        int backgroundColor = a.getColor(
461f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee                R.styleable.ContactListItemView_list_item_background_color, Color.WHITE);
476fdf16941587a222f554482fb72531783a924b6eAndrew Lee        int textOffsetTop = a.getDimensionPixelSize(
486fdf16941587a222f554482fb72531783a924b6eAndrew Lee                R.styleable.ContactListItemView_list_item_text_offset_top, 0);
4966ee56383afe4bafa38fa0babdd0dfd07cc7c7b2Andrew Lee        int paddingStartOffset = a.getDimensionPixelSize(
5066ee56383afe4bafa38fa0babdd0dfd07cc7c7b2Andrew Lee                R.styleable.ContactListItemView_list_item_padding_left, 0);
51707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell        int textWidth = getResources().getDimensionPixelSize(
52707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell                R.dimen.contact_list_section_header_width);
53707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell        int widthIncludingPadding = paddingStartOffset + textWidth;
54d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        a.recycle();
55d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
561f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee        setBackgroundColor(backgroundColor);
57ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Lee        setTextAppearance(getContext(), R.style.SectionHeaderStyle);
58707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell        setLayoutParams(new LayoutParams(widthIncludingPadding, LayoutParams.WRAP_CONTENT));
5953c7dc1e9691b5b427395642a4274d37a1a15bb4Yorke Lee        setLayoutDirection(parent.getLayoutDirection());
60ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Lee        setGravity(Gravity.CENTER_VERTICAL |
61ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Lee                (ViewUtil.isViewLayoutRtl(this) ? Gravity.RIGHT : Gravity.LEFT));
626fdf16941587a222f554482fb72531783a924b6eAndrew Lee
636fdf16941587a222f554482fb72531783a924b6eAndrew Lee        // Apply text top offset. Multiply by two, because we are implementing this by padding for a
646fdf16941587a222f554482fb72531783a924b6eAndrew Lee        // vertically centered view, rather than adjusting the position directly via a layout.
656fdf16941587a222f554482fb72531783a924b6eAndrew Lee        setPaddingRelative(
6666ee56383afe4bafa38fa0babdd0dfd07cc7c7b2Andrew Lee                getPaddingStart() + paddingStartOffset,
676fdf16941587a222f554482fb72531783a924b6eAndrew Lee                getPaddingTop() + (textOffsetTop * 2),
686fdf16941587a222f554482fb72531783a924b6eAndrew Lee                getPaddingEnd(),
696fdf16941587a222f554482fb72531783a924b6eAndrew Lee                getPaddingBottom());
70d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng    }
71d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
72d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng    /**
73d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng     * Sets section header or makes it invisible if the title is null.
74d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng     */
751f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee    public void setSectionHeaderTitle(String title) {
76d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        if (!TextUtils.isEmpty(title)) {
771f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee            setText(title);
781f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee            setVisibility(View.VISIBLE);
79d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        } else {
801f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee            setVisibility(View.GONE);
81d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        }
82d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng    }
83d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng}
84