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;
29import com.android.mail.utils.ViewUtils;
30
31import 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 */
38public class SuperCollapsedBlock extends FrameLayout implements View.OnClickListener {
39
40    public interface OnClickListener {
41        /**
42         * Handle a click on a super-collapsed block.
43         *
44         */
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
55    public SuperCollapsedBlock(Context context) {
56        this(context, null);
57    }
58
59    public SuperCollapsedBlock(Context context, AttributeSet attrs) {
60        super(context, attrs);
61        setActivated(false);
62        setOnClickListener(this);
63    }
64
65    public void initialize(OnClickListener onClick) {
66        mClick = onClick;
67    }
68
69    @Override
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
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
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
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