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