SuperCollapsedBlock.java revision 46dfba6160b55a582b344328067e3dafeb881dd9
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;
31import com.android.mail.utils.LogUtils;
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        HeaderBlock {
40
41    public interface OnClickListener {
42        /**
43         * Handle a click on a super-collapsed block.
44         *
45         */
46        void onSuperCollapsedClick(SuperCollapsedBlockItem item);
47    }
48
49    private SuperCollapsedBlockItem mModel;
50    private OnClickListener mClick;
51    private View mIconView;
52    private TextView mCountView;
53    private View mBackgroundView;
54
55    private static final String LOG_TAG = new LogUtils().getLogTag();
56
57    public SuperCollapsedBlock(Context context) {
58        this(context, null);
59    }
60
61    public SuperCollapsedBlock(Context context, AttributeSet attrs) {
62        super(context, attrs);
63        setActivated(false);
64        setOnClickListener(this);
65    }
66
67    public void initialize(OnClickListener onClick) {
68        mClick = onClick;
69    }
70
71    @Override
72    protected void onFinishInflate() {
73        super.onFinishInflate();
74
75        mIconView = findViewById(R.id.super_collapsed_icon);
76        mCountView = (TextView) findViewById(R.id.super_collapsed_count);
77        mBackgroundView = findViewById(R.id.super_collapsed_background);
78
79        // Work around Honeycomb bug where BitmapDrawable's tileMode is unreliable in XML (5160739)
80        BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(
81                R.drawable.header_convo_view_thread_bg_holo);
82        bd.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
83        mBackgroundView.setBackgroundDrawable(bd);
84    }
85
86    public void bind(SuperCollapsedBlockItem item) {
87        mModel = item;
88        setCount(item.getEnd() - item.getStart() + 1);
89    }
90
91    public void setCount(int count) {
92        mCountView.setText(Integer.toString(count));
93        mIconView.getBackground().setLevel(count);
94    }
95
96    @Override
97    public void onClick(final View v) {
98        ((TextView) findViewById(R.id.super_collapsed_label)).setText(
99                R.string.loading_conversation);
100        mCountView.setVisibility(GONE);
101
102        if (mClick != null) {
103            getHandler().post(new Runnable() {
104                @Override
105                public void run() {
106                    mClick.onSuperCollapsedClick(mModel);
107                }
108            });
109        }
110    }
111
112    public static int getCannedHeight(Context context) {
113        Resources r = context.getResources();
114        // Rather than try to measure the height a super-collapsed block, just add up the known
115        // vertical dimension components.
116        return r.getDimensionPixelSize(R.dimen.super_collapsed_height)
117                + r.getDimensionPixelOffset(R.dimen.message_header_vertical_margin);
118    }
119
120    @Override
121    public boolean canSnap() {
122        return false;
123    }
124
125    @Override
126    public MessageHeaderView getSnapView() {
127        return null;
128    }
129
130    @Override
131    public void setMarginBottom(int height) {
132        // no-op. should never have a matching body.
133
134        // sanity check
135        if (height != 0) {
136            LogUtils.d(LOG_TAG, "super-collapsed block yielded unexpected body height: %d", height);
137        }
138    }
139
140    @Override
141    public void updateContactInfo() {
142        // no-op
143    }
144
145    @Override
146    public void setStarDisplay(boolean starred) {
147        // no-op
148    }
149
150}
151