Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.list; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.graphics.Color; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 23 | import android.view.Gravity; |
| 24 | import android.view.View; |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 25 | import android.widget.LinearLayout.LayoutParams; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 26 | import android.widget.TextView; |
| 27 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 28 | import com.android.contacts.R; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * A custom view for the pinned section header shown at the top of the contact list. |
| 32 | */ |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 33 | public class ContactListPinnedHeaderView extends TextView { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 34 | |
Yorke Lee | 525f513 | 2014-07-30 18:59:28 -0700 | [diff] [blame] | 35 | public ContactListPinnedHeaderView(Context context, AttributeSet attrs, View parent) { |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 36 | super(context, attrs); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 37 | |
Yorke Lee | a675e3a | 2015-10-26 19:43:22 -0700 | [diff] [blame] | 38 | if (R.styleable.ContactListItemView == null) { |
| 39 | return; |
| 40 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 41 | TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ContactListItemView); |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 42 | int backgroundColor = a.getColor( |
| 43 | R.styleable.ContactListItemView_list_item_background_color, Color.WHITE); |
Andrew Lee | 815ce48 | 2014-05-30 12:28:17 -0700 | [diff] [blame] | 44 | int textOffsetTop = a.getDimensionPixelSize( |
| 45 | R.styleable.ContactListItemView_list_item_text_offset_top, 0); |
Andrew Lee | 0861aad | 2014-06-09 14:02:51 -0700 | [diff] [blame] | 46 | int paddingStartOffset = a.getDimensionPixelSize( |
| 47 | R.styleable.ContactListItemView_list_item_padding_left, 0); |
Brian Attwell | ab538e5 | 2014-09-04 20:58:15 -0700 | [diff] [blame] | 48 | int textWidth = getResources().getDimensionPixelSize( |
| 49 | R.dimen.contact_list_section_header_width); |
| 50 | int widthIncludingPadding = paddingStartOffset + textWidth; |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 51 | a.recycle(); |
| 52 | |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 53 | setBackgroundColor(backgroundColor); |
Andrew Lee | dd28d83 | 2014-04-28 18:38:31 -0700 | [diff] [blame] | 54 | setTextAppearance(getContext(), R.style.SectionHeaderStyle); |
Brian Attwell | ab538e5 | 2014-09-04 20:58:15 -0700 | [diff] [blame] | 55 | setLayoutParams(new LayoutParams(widthIncludingPadding, LayoutParams.WRAP_CONTENT)); |
Yorke Lee | 525f513 | 2014-07-30 18:59:28 -0700 | [diff] [blame] | 56 | setLayoutDirection(parent.getLayoutDirection()); |
Wenyi Wang | d5403fa | 2016-07-20 17:16:38 -0700 | [diff] [blame] | 57 | setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); |
Andrew Lee | 815ce48 | 2014-05-30 12:28:17 -0700 | [diff] [blame] | 58 | |
| 59 | // Apply text top offset. Multiply by two, because we are implementing this by padding for a |
| 60 | // vertically centered view, rather than adjusting the position directly via a layout. |
| 61 | setPaddingRelative( |
Andrew Lee | 0861aad | 2014-06-09 14:02:51 -0700 | [diff] [blame] | 62 | getPaddingStart() + paddingStartOffset, |
Andrew Lee | 815ce48 | 2014-05-30 12:28:17 -0700 | [diff] [blame] | 63 | getPaddingTop() + (textOffsetTop * 2), |
| 64 | getPaddingEnd(), |
| 65 | getPaddingBottom()); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets section header or makes it invisible if the title is null. |
| 70 | */ |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 71 | public void setSectionHeaderTitle(String title) { |
John Shao | da18871 | 2016-08-19 14:57:11 -0700 | [diff] [blame] | 72 | if (title != null) { |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 73 | setText(title); |
| 74 | setVisibility(View.VISIBLE); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 75 | } else { |
Andrew Lee | 020ba62 | 2014-04-25 15:26:35 -0700 | [diff] [blame] | 76 | setVisibility(View.GONE); |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
Chiao Cheng | 89437e8 | 2012-11-01 13:41:51 -0700 | [diff] [blame] | 79 | } |