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