SuperCollapsedBlock.java revision 9e2d407fdafeb874e640eb84017feaf784309075
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.graphics.Shader.TileMode;
23import android.graphics.drawable.BitmapDrawable;
24import android.util.AttributeSet;
25import android.view.View;
26import android.widget.FrameLayout;
27import android.widget.TextView;
28
29import com.android.mail.R;
30import com.android.mail.browse.ConversationViewAdapter.SuperCollapsedBlockItem;
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 mModel;
48    private OnClickListener mClick;
49    private View mIconView;
50    private TextView mCountView;
51    private View mBackgroundView;
52
53    public SuperCollapsedBlock(Context context) {
54        this(context, null);
55    }
56
57    public SuperCollapsedBlock(Context context, AttributeSet attrs) {
58        super(context, attrs);
59        setActivated(false);
60        setOnClickListener(this);
61    }
62
63    public void initialize(OnClickListener onClick) {
64        mClick = onClick;
65    }
66
67    @Override
68    protected void onFinishInflate() {
69        super.onFinishInflate();
70
71        mIconView = findViewById(R.id.super_collapsed_icon);
72        mCountView = (TextView) findViewById(R.id.super_collapsed_count);
73        mBackgroundView = findViewById(R.id.super_collapsed_background);
74
75        // Work around Honeycomb bug where BitmapDrawable's tileMode is unreliable in XML (5160739)
76        BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(
77                R.drawable.header_convo_view_thread_bg_holo);
78        bd.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
79        mBackgroundView.setBackgroundDrawable(bd);
80    }
81
82    public void bind(SuperCollapsedBlockItem item) {
83        mModel = item;
84        setCount(item.getEnd() - item.getStart() + 1);
85    }
86
87    public void setCount(int count) {
88        mCountView.setText(Integer.toString(count));
89        mIconView.getBackground().setLevel(count);
90    }
91
92    @Override
93    public void onClick(final View v) {
94        ((TextView) findViewById(R.id.super_collapsed_label)).setText(
95                R.string.loading_conversation);
96        mCountView.setVisibility(GONE);
97
98        if (mClick != null) {
99            getHandler().post(new Runnable() {
100                @Override
101                public void run() {
102                    mClick.onSuperCollapsedClick(mModel);
103                }
104            });
105        }
106    }
107
108    public static int getCannedHeight(Context context) {
109        Resources r = context.getResources();
110        // Rather than try to measure the height a super-collapsed block, just add up the known
111        // vertical dimension components.
112        return r.getDimensionPixelSize(R.dimen.super_collapsed_height)
113                + r.getDimensionPixelOffset(R.dimen.message_header_vertical_margin);
114    }
115
116}
117