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
41d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView);
421f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee        int backgroundColor = a.getColor(
431f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee                R.styleable.ContactListItemView_list_item_background_color, Color.WHITE);
446fdf16941587a222f554482fb72531783a924b6eAndrew Lee        int textOffsetTop = a.getDimensionPixelSize(
456fdf16941587a222f554482fb72531783a924b6eAndrew Lee                R.styleable.ContactListItemView_list_item_text_offset_top, 0);
4666ee56383afe4bafa38fa0babdd0dfd07cc7c7b2Andrew Lee        int paddingStartOffset = a.getDimensionPixelSize(
4766ee56383afe4bafa38fa0babdd0dfd07cc7c7b2Andrew Lee                R.styleable.ContactListItemView_list_item_padding_left, 0);
48707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell        int textWidth = getResources().getDimensionPixelSize(
49707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell                R.dimen.contact_list_section_header_width);
50707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell        int widthIncludingPadding = paddingStartOffset + textWidth;
51d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        a.recycle();
52d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
531f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee        setBackgroundColor(backgroundColor);
54ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Lee        setTextAppearance(getContext(), R.style.SectionHeaderStyle);
55707bfd0095a4994ef32ae65e780b23881b54eebfBrian Attwell        setLayoutParams(new LayoutParams(widthIncludingPadding, LayoutParams.WRAP_CONTENT));
5653c7dc1e9691b5b427395642a4274d37a1a15bb4Yorke Lee        setLayoutDirection(parent.getLayoutDirection());
57ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Lee        setGravity(Gravity.CENTER_VERTICAL |
58ceb851d9c2d5c73bdd9c60ca47be0902516636aaAndrew Lee                (ViewUtil.isViewLayoutRtl(this) ? Gravity.RIGHT : Gravity.LEFT));
596fdf16941587a222f554482fb72531783a924b6eAndrew Lee
606fdf16941587a222f554482fb72531783a924b6eAndrew Lee        // Apply text top offset. Multiply by two, because we are implementing this by padding for a
616fdf16941587a222f554482fb72531783a924b6eAndrew Lee        // vertically centered view, rather than adjusting the position directly via a layout.
626fdf16941587a222f554482fb72531783a924b6eAndrew Lee        setPaddingRelative(
6366ee56383afe4bafa38fa0babdd0dfd07cc7c7b2Andrew Lee                getPaddingStart() + paddingStartOffset,
646fdf16941587a222f554482fb72531783a924b6eAndrew Lee                getPaddingTop() + (textOffsetTop * 2),
656fdf16941587a222f554482fb72531783a924b6eAndrew Lee                getPaddingEnd(),
666fdf16941587a222f554482fb72531783a924b6eAndrew Lee                getPaddingBottom());
67d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng    }
68d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng
69d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng    /**
70d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng     * Sets section header or makes it invisible if the title is null.
71d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng     */
721f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee    public void setSectionHeaderTitle(String title) {
73d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        if (!TextUtils.isEmpty(title)) {
741f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee            setText(title);
751f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee            setVisibility(View.VISIBLE);
76d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        } else {
771f1e90d36fe914a7ce51e6f9884199580115d891Andrew Lee            setVisibility(View.GONE);
78d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng        }
79d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng    }
80d6bba124836ec2f528b329759e38fda6297fec49Chiao Cheng}
81