1 /*
2  * Copyright (C) 2012 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mail.browse;
19 
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.FrameLayout;
25 import android.widget.TextView;
26 
27 import com.android.mail.R;
28 import com.android.mail.browse.ConversationViewAdapter.SuperCollapsedBlockItem;
29 import com.android.mail.utils.ViewUtils;
30 
31 import java.text.NumberFormat;
32 
33 /**
34  * A header block that expands to a list of collapsed message headers. Will notify a listener on tap
35  * so the listener can hide the block and reveal the corresponding collapsed message headers.
36  *
37  */
38 public class SuperCollapsedBlock extends FrameLayout implements View.OnClickListener {
39 
40     public interface OnClickListener {
41         /**
42          * Handle a click on a super-collapsed block.
43          *
44          */
onSuperCollapsedClick(SuperCollapsedBlockItem item)45         void onSuperCollapsedClick(SuperCollapsedBlockItem item);
46     }
47 
48     private SuperCollapsedBlockItem mSuperCollapsedItem;
49     private OnClickListener mClick;
50     private TextView mSuperCollapsedText;
51     private View mSuperCollapsedProgress;
52 
53     private int mCount;
54 
SuperCollapsedBlock(Context context)55     public SuperCollapsedBlock(Context context) {
56         this(context, null);
57     }
58 
SuperCollapsedBlock(Context context, AttributeSet attrs)59     public SuperCollapsedBlock(Context context, AttributeSet attrs) {
60         super(context, attrs);
61         setActivated(false);
62         setOnClickListener(this);
63     }
64 
initialize(OnClickListener onClick)65     public void initialize(OnClickListener onClick) {
66         mClick = onClick;
67     }
68 
69     @Override
onFinishInflate()70     protected void onFinishInflate() {
71         super.onFinishInflate();
72         mSuperCollapsedText = (TextView) findViewById(R.id.super_collapsed_text);
73         mSuperCollapsedProgress = findViewById(R.id.super_collapsed_progress);
74     }
75 
bind(SuperCollapsedBlockItem item)76     public void bind(SuperCollapsedBlockItem item) {
77         mSuperCollapsedItem = item;
78         mSuperCollapsedText.setVisibility(VISIBLE);
79         mSuperCollapsedProgress.setVisibility(GONE);
80         setCount(item.getEnd() - item.getStart() + 1);
81     }
82 
setCount(int count)83     public void setCount(int count) {
84         mCount = count;
85         final String strCount = NumberFormat.getIntegerInstance().format(mCount);
86         mSuperCollapsedText.setText(strCount);
87         final Resources res = getResources();
88         final int colorId = mSuperCollapsedItem.hasDraft() ?
89                 R.color.text_color_draft_red : R.color.conversation_view_text_color_light;
90         mSuperCollapsedText.setTextColor(res.getColor(colorId));
91         setContentDescription(
92                 res.getQuantityString(R.plurals.show_messages_read, mCount, mCount));
93     }
94 
95     @Override
onClick(final View v)96     public void onClick(final View v) {
97         mSuperCollapsedText.setVisibility(GONE);
98         mSuperCollapsedProgress.setVisibility(VISIBLE);
99         final String announcement = getResources().getQuantityString(
100                 R.plurals.super_collapsed_block_accessibility_announcement, mCount, mCount);
101         ViewUtils.announceForAccessibility(this, announcement);
102 
103         if (mClick != null) {
104             getHandler().post(new Runnable() {
105                 @Override
106                 public void run() {
107                     mClick.onSuperCollapsedClick(mSuperCollapsedItem);
108                 }
109             });
110         }
111     }
112 }
113