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
18package com.android.mail.browse;
19
20import android.content.Context;
21import android.content.res.Resources;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.FrameLayout;
25import android.widget.TextView;
26
27import com.android.mail.R;
28import com.android.mail.browse.ConversationViewAdapter.SuperCollapsedBlockItem;
29
30import java.text.NumberFormat;
31
32/**
33 * A header block that expands to a list of collapsed message headers. Will notify a listener on tap
34 * so the listener can hide the block and reveal the corresponding collapsed message headers.
35 *
36 */
37public class SuperCollapsedBlock extends FrameLayout implements View.OnClickListener {
38
39    public interface OnClickListener {
40        /**
41         * Handle a click on a super-collapsed block.
42         *
43         */
44        void onSuperCollapsedClick(SuperCollapsedBlockItem item);
45    }
46
47    private SuperCollapsedBlockItem mSuperCollapsedItem;
48    private OnClickListener mClick;
49    private TextView mSuperCollapsedText;
50
51    public SuperCollapsedBlock(Context context) {
52        this(context, null);
53    }
54
55    public SuperCollapsedBlock(Context context, AttributeSet attrs) {
56        super(context, attrs);
57        setActivated(false);
58        setOnClickListener(this);
59    }
60
61    public void initialize(OnClickListener onClick) {
62        mClick = onClick;
63    }
64
65    @Override
66    protected void onFinishInflate() {
67        super.onFinishInflate();
68        mSuperCollapsedText = (TextView) findViewById(R.id.super_collapsed_text);
69    }
70
71    public void bind(SuperCollapsedBlockItem item) {
72        mSuperCollapsedItem = item;
73        setCount(item.getEnd() - item.getStart() + 1);
74    }
75
76    public void setCount(int count) {
77        final String strCount = NumberFormat.getIntegerInstance().format(count);
78        mSuperCollapsedText.setText(strCount);
79        final Resources res = getResources();
80        final int colorId = mSuperCollapsedItem.hasDraft() ?
81                R.color.text_color_draft_red : R.color.conversation_view_text_color_light;
82        mSuperCollapsedText.setTextColor(res.getColor(colorId));
83        setContentDescription(
84                res.getQuantityString(R.plurals.show_messages_read, count, count));
85    }
86
87    @Override
88    public void onClick(final View v) {
89        ((TextView) findViewById(R.id.super_collapsed_text)).setText(
90                R.string.loading_conversation);
91
92        if (mClick != null) {
93            getHandler().post(new Runnable() {
94                @Override
95                public void run() {
96                    mClick.onSuperCollapsedClick(mSuperCollapsedItem);
97                }
98            });
99        }
100    }
101}
102