Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 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 | |
| 18 | package com.android.mail.browse; |
| 19 | |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 20 | import android.app.FragmentManager; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 21 | import android.app.LoaderManager; |
| 22 | import android.content.Context; |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 23 | import android.view.Gravity; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 27 | import android.widget.BaseAdapter; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 28 | |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 29 | import com.android.mail.ContactInfoSource; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 30 | import com.android.mail.FormattedDateBuilder; |
| 31 | import com.android.mail.R; |
| 32 | import com.android.mail.browse.ConversationViewHeader.ConversationViewHeaderCallbacks; |
| 33 | import com.android.mail.browse.MessageHeaderView.MessageHeaderViewCallbacks; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 34 | import com.android.mail.browse.SuperCollapsedBlock.OnClickListener; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 35 | import com.android.mail.providers.Address; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 36 | import com.android.mail.providers.Conversation; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 37 | import com.android.mail.providers.UIProvider; |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 38 | import com.android.mail.ui.ControllableActivity; |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 39 | import com.android.mail.utils.VeiledAddressMatcher; |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 40 | import com.google.common.base.Objects; |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 41 | import com.google.common.collect.Lists; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 42 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 43 | import java.util.Collection; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 44 | import java.util.List; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 45 | import java.util.Map; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * A specialized adapter that contains overlay views to draw on top of the underlying conversation |
| 49 | * WebView. Each independently drawn overlay view gets its own item in this adapter, and indices |
| 50 | * in this adapter do not necessarily line up with cursor indices. For example, an expanded |
| 51 | * message may have a header and footer, and since they are not drawn coupled together, they each |
| 52 | * get an adapter item. |
| 53 | * <p> |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 54 | * Each item in this adapter is a {@link ConversationOverlayItem} to expose enough information |
| 55 | * to {@link ConversationContainer} so that it can position overlays properly. |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 56 | * |
| 57 | */ |
| 58 | public class ConversationViewAdapter extends BaseAdapter { |
| 59 | |
| 60 | private Context mContext; |
| 61 | private final FormattedDateBuilder mDateBuilder; |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 62 | private final ConversationAccountController mAccountController; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 63 | private final LoaderManager mLoaderManager; |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 64 | private final FragmentManager mFragmentManager; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 65 | private final MessageHeaderViewCallbacks mMessageCallbacks; |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 66 | private final ContactInfoSource mContactInfoSource; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 67 | private ConversationViewHeaderCallbacks mConversationCallbacks; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 68 | private OnClickListener mSuperCollapsedListener; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 69 | private Map<String, Address> mAddressCache; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 70 | private final LayoutInflater mInflater; |
| 71 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 72 | private final List<ConversationOverlayItem> mItems; |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 73 | private final VeiledAddressMatcher mMatcher; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 74 | |
| 75 | public static final int VIEW_TYPE_CONVERSATION_HEADER = 0; |
Andrew Sapperstein | 9f957f3 | 2013-07-19 15:18:18 -0700 | [diff] [blame] | 76 | public static final int VIEW_TYPE_AD_HEADER = 1; |
Andrew Sapperstein | 606dbd7 | 2013-07-30 19:14:23 -0700 | [diff] [blame] | 77 | public static final int VIEW_TYPE_MESSAGE_HEADER = 2; |
| 78 | public static final int VIEW_TYPE_MESSAGE_FOOTER = 3; |
| 79 | public static final int VIEW_TYPE_SUPER_COLLAPSED_BLOCK = 4; |
| 80 | public static final int VIEW_TYPE_BORDER = 5; |
| 81 | public static final int VIEW_TYPE_COUNT = 6; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 82 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 83 | public class ConversationHeaderItem extends ConversationOverlayItem { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 84 | public final Conversation mConversation; |
| 85 | |
| 86 | private ConversationHeaderItem(Conversation conv) { |
| 87 | mConversation = conv; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int getType() { |
| 92 | return VIEW_TYPE_CONVERSATION_HEADER; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public View createView(Context context, LayoutInflater inflater, ViewGroup parent) { |
| 97 | final ConversationViewHeader headerView = (ConversationViewHeader) inflater.inflate( |
| 98 | R.layout.conversation_view_header, parent, false); |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 99 | headerView.setCallbacks(mConversationCallbacks, mAccountController); |
Mark Wei | ab2d998 | 2012-09-25 13:06:17 -0700 | [diff] [blame] | 100 | headerView.bind(this); |
Andy Huang | 7388dba | 2012-09-25 18:30:28 -0700 | [diff] [blame] | 101 | headerView.setSubject(mConversation.subject); |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 102 | if (mAccountController.getAccount().supportsCapability( |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 103 | UIProvider.AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV)) { |
Andy Huang | 7388dba | 2012-09-25 18:30:28 -0700 | [diff] [blame] | 104 | headerView.setFolders(mConversation); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return headerView; |
| 108 | } |
| 109 | |
| 110 | @Override |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 111 | public void bindView(View v, boolean measureOnly) { |
Mark Wei | ab2d998 | 2012-09-25 13:06:17 -0700 | [diff] [blame] | 112 | ConversationViewHeader header = (ConversationViewHeader) v; |
| 113 | header.bind(this); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 117 | public boolean isContiguous() { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 123 | public static class MessageHeaderItem extends ConversationOverlayItem { |
| 124 | |
| 125 | private final ConversationViewAdapter mAdapter; |
| 126 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 127 | private ConversationMessage mMessage; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 128 | |
| 129 | // view state variables |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 130 | private boolean mExpanded; |
| 131 | public boolean detailsExpanded; |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 132 | private boolean mShowImages; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 133 | |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 134 | // cached values to speed up re-rendering during view recycling |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 135 | private CharSequence mTimestampShort; |
| 136 | private CharSequence mTimestampLong; |
| 137 | private long mTimestampMs; |
| 138 | private FormattedDateBuilder mDateBuilder; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 139 | public CharSequence recipientSummaryText; |
| 140 | |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 141 | MessageHeaderItem(ConversationViewAdapter adapter, FormattedDateBuilder dateBuilder, |
| 142 | ConversationMessage message, boolean expanded, boolean showImages) { |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 143 | mAdapter = adapter; |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 144 | mDateBuilder = dateBuilder; |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 145 | mMessage = message; |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 146 | mExpanded = expanded; |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 147 | mShowImages = showImages; |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 148 | |
| 149 | detailsExpanded = false; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 152 | public ConversationMessage getMessage() { |
| 153 | return mMessage; |
| 154 | } |
| 155 | |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public int getType() { |
| 158 | return VIEW_TYPE_MESSAGE_HEADER; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public View createView(Context context, LayoutInflater inflater, ViewGroup parent) { |
| 163 | final MessageHeaderView v = (MessageHeaderView) inflater.inflate( |
| 164 | R.layout.conversation_message_header, parent, false); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 165 | v.initialize(mAdapter.mAccountController, |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 166 | mAdapter.mAddressCache); |
| 167 | v.setCallbacks(mAdapter.mMessageCallbacks); |
| 168 | v.setContactInfoSource(mAdapter.mContactInfoSource); |
| 169 | v.setVeiledMatcher(mAdapter.mMatcher); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 170 | return v; |
| 171 | } |
| 172 | |
| 173 | @Override |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 174 | public void bindView(View v, boolean measureOnly) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 175 | final MessageHeaderView header = (MessageHeaderView) v; |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 176 | header.bind(this, measureOnly); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 180 | public void onModelUpdated(View v) { |
| 181 | final MessageHeaderView header = (MessageHeaderView) v; |
| 182 | header.refresh(); |
| 183 | } |
| 184 | |
| 185 | @Override |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 186 | public boolean isContiguous() { |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 187 | return !isExpanded(); |
| 188 | } |
| 189 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 190 | @Override |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 191 | public boolean isExpanded() { |
| 192 | return mExpanded; |
| 193 | } |
| 194 | |
| 195 | public void setExpanded(boolean expanded) { |
| 196 | if (mExpanded != expanded) { |
| 197 | mExpanded = expanded; |
| 198 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 199 | } |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 200 | |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 201 | public boolean getShowImages() { |
| 202 | return mShowImages; |
| 203 | } |
| 204 | |
| 205 | public void setShowImages(boolean showImages) { |
| 206 | mShowImages = showImages; |
| 207 | } |
| 208 | |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 209 | @Override |
| 210 | public boolean canBecomeSnapHeader() { |
| 211 | return isExpanded(); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public boolean canPushSnapHeader() { |
| 216 | return true; |
| 217 | } |
| 218 | |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public boolean belongsToMessage(ConversationMessage message) { |
| 221 | return Objects.equal(mMessage, message); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void setMessage(ConversationMessage message) { |
| 226 | mMessage = message; |
| 227 | } |
| 228 | |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 229 | public CharSequence getTimestampShort() { |
| 230 | ensureTimestamps(); |
| 231 | return mTimestampShort; |
| 232 | } |
| 233 | |
| 234 | public CharSequence getTimestampLong() { |
| 235 | ensureTimestamps(); |
| 236 | return mTimestampLong; |
| 237 | } |
| 238 | |
| 239 | private void ensureTimestamps() { |
| 240 | if (mMessage.dateReceivedMs != mTimestampMs) { |
| 241 | mTimestampMs = mMessage.dateReceivedMs; |
| 242 | mTimestampShort = mDateBuilder.formatShortDate(mTimestampMs); |
| 243 | mTimestampLong = mDateBuilder.formatLongDateTime(mTimestampMs); |
| 244 | } |
| 245 | } |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 246 | |
| 247 | public ConversationViewAdapter getAdapter() { |
| 248 | return mAdapter; |
| 249 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 252 | public class MessageFooterItem extends ConversationOverlayItem { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 253 | /** |
| 254 | * A footer can only exist if there is a matching header. Requiring a header allows a |
| 255 | * footer to stay in sync with the expanded state of the header. |
| 256 | */ |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 257 | private final MessageHeaderItem mHeaderitem; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 258 | |
| 259 | private MessageFooterItem(MessageHeaderItem item) { |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 260 | mHeaderitem = item; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public int getType() { |
| 265 | return VIEW_TYPE_MESSAGE_FOOTER; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public View createView(Context context, LayoutInflater inflater, ViewGroup parent) { |
Andy Huang | f0e95c3 | 2012-04-02 18:11:17 -0700 | [diff] [blame] | 270 | final MessageFooterView v = (MessageFooterView) inflater.inflate( |
| 271 | R.layout.conversation_message_footer, parent, false); |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 272 | v.initialize(mLoaderManager, mFragmentManager); |
Andy Huang | f0e95c3 | 2012-04-02 18:11:17 -0700 | [diff] [blame] | 273 | return v; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 277 | public void bindView(View v, boolean measureOnly) { |
Andy Huang | f0e95c3 | 2012-04-02 18:11:17 -0700 | [diff] [blame] | 278 | final MessageFooterView attachmentsView = (MessageFooterView) v; |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 279 | attachmentsView.bind(mHeaderitem, mAccountController.getAccount().uri, measureOnly); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public boolean isContiguous() { |
| 284 | return true; |
| 285 | } |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 286 | |
| 287 | @Override |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 288 | public boolean isExpanded() { |
| 289 | return mHeaderitem.isExpanded(); |
| 290 | } |
| 291 | |
| 292 | @Override |
Andy Huang | adbf3e8 | 2012-10-13 13:30:19 -0700 | [diff] [blame] | 293 | public int getGravity() { |
| 294 | // attachments are top-aligned within their spacer area |
| 295 | // Attachments should stay near the body they belong to, even when zoomed far in. |
| 296 | return Gravity.TOP; |
| 297 | } |
| 298 | |
| 299 | @Override |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 300 | public int getHeight() { |
| 301 | // a footer may change height while its view does not exist because it is offscreen |
| 302 | // (but the header is onscreen and thus collapsible) |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 303 | if (!mHeaderitem.isExpanded()) { |
Andy Huang | c754357 | 2012-04-03 15:34:29 -0700 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | return super.getHeight(); |
| 307 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 310 | public class SuperCollapsedBlockItem extends ConversationOverlayItem { |
| 311 | |
| 312 | private final int mStart; |
| 313 | private int mEnd; |
| 314 | |
| 315 | private SuperCollapsedBlockItem(int start, int end) { |
| 316 | mStart = start; |
| 317 | mEnd = end; |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public int getType() { |
| 322 | return VIEW_TYPE_SUPER_COLLAPSED_BLOCK; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public View createView(Context context, LayoutInflater inflater, ViewGroup parent) { |
| 327 | final SuperCollapsedBlock scb = (SuperCollapsedBlock) inflater.inflate( |
| 328 | R.layout.super_collapsed_block, parent, false); |
| 329 | scb.initialize(mSuperCollapsedListener); |
| 330 | return scb; |
| 331 | } |
| 332 | |
| 333 | @Override |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 334 | public void bindView(View v, boolean measureOnly) { |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 335 | final SuperCollapsedBlock scb = (SuperCollapsedBlock) v; |
| 336 | scb.bind(this); |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public boolean isContiguous() { |
| 341 | return true; |
| 342 | } |
| 343 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public boolean isExpanded() { |
| 346 | return false; |
| 347 | } |
| 348 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 349 | public int getStart() { |
| 350 | return mStart; |
| 351 | } |
| 352 | |
| 353 | public int getEnd() { |
| 354 | return mEnd; |
| 355 | } |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 356 | |
| 357 | @Override |
| 358 | public boolean canPushSnapHeader() { |
| 359 | return true; |
| 360 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 363 | |
| 364 | public class BorderItem extends ConversationOverlayItem { |
Andrew Sapperstein | 12c03aa | 2013-07-25 17:15:22 -0700 | [diff] [blame] | 365 | private final boolean mContiguous; |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 366 | private boolean mExpanded; |
Andrew Sapperstein | e539880 | 2013-07-29 20:55:38 -0700 | [diff] [blame] | 367 | private final boolean mFirstBorder; |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 368 | private boolean mLastBorder; |
Andrew Sapperstein | 12c03aa | 2013-07-25 17:15:22 -0700 | [diff] [blame] | 369 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 370 | public BorderItem(boolean contiguous, boolean isExpanded, |
| 371 | boolean firstBorder, boolean lastBorder) { |
Andrew Sapperstein | 12c03aa | 2013-07-25 17:15:22 -0700 | [diff] [blame] | 372 | mContiguous = contiguous; |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 373 | mExpanded = isExpanded; |
Andrew Sapperstein | e539880 | 2013-07-29 20:55:38 -0700 | [diff] [blame] | 374 | mFirstBorder = firstBorder; |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 375 | mLastBorder = lastBorder; |
Andrew Sapperstein | 12c03aa | 2013-07-25 17:15:22 -0700 | [diff] [blame] | 376 | } |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 377 | |
| 378 | @Override |
| 379 | public int getType() { |
| 380 | return VIEW_TYPE_BORDER; |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public View createView(Context context, LayoutInflater inflater, ViewGroup parent) { |
| 385 | return inflater.inflate(R.layout.card_border, parent, false); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void bindView(View v, boolean measureOnly) { |
Andrew Sapperstein | e539880 | 2013-07-29 20:55:38 -0700 | [diff] [blame] | 390 | final BorderView border = (BorderView) v; |
| 391 | border.bind(this, measureOnly); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public boolean isContiguous() { |
Andrew Sapperstein | 12c03aa | 2013-07-25 17:15:22 -0700 | [diff] [blame] | 396 | return mContiguous; |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 400 | public boolean isExpanded() { |
| 401 | return mExpanded; |
| 402 | } |
| 403 | |
| 404 | public void setExpanded(boolean isExpanded) { |
| 405 | mExpanded = isExpanded; |
| 406 | } |
| 407 | |
| 408 | @Override |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 409 | public boolean canPushSnapHeader() { |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 410 | return false; |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 411 | } |
Andrew Sapperstein | e539880 | 2013-07-29 20:55:38 -0700 | [diff] [blame] | 412 | |
| 413 | public boolean isFirstBorder() { |
| 414 | return mFirstBorder; |
| 415 | } |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 416 | |
| 417 | public boolean isLastBorder() { |
| 418 | return mLastBorder; |
| 419 | } |
| 420 | |
| 421 | public void setIsLastBorder(boolean isLastBorder) { |
| 422 | mLastBorder = isLastBorder; |
| 423 | } |
| 424 | |
| 425 | public ConversationViewAdapter getAdapter() { |
| 426 | return ConversationViewAdapter.this; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void rebindView(View view) { |
| 431 | bindView(view, false); |
| 432 | } |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 435 | public ConversationViewAdapter(ControllableActivity controllableActivity, |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 436 | ConversationAccountController accountController, |
| 437 | LoaderManager loaderManager, |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 438 | MessageHeaderViewCallbacks messageCallbacks, |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 439 | ContactInfoSource contactInfoSource, |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 440 | ConversationViewHeaderCallbacks convCallbacks, |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 441 | SuperCollapsedBlock.OnClickListener scbListener, Map<String, Address> addressCache, |
| 442 | FormattedDateBuilder dateBuilder) { |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 443 | mContext = controllableActivity.getActivityContext(); |
Andy Huang | 59e0b18 | 2012-08-14 14:32:23 -0700 | [diff] [blame] | 444 | mDateBuilder = dateBuilder; |
Andy Huang | 28b7aee | 2012-08-20 20:27:32 -0700 | [diff] [blame] | 445 | mAccountController = accountController; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 446 | mLoaderManager = loaderManager; |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 447 | mFragmentManager = controllableActivity.getFragmentManager(); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 448 | mMessageCallbacks = messageCallbacks; |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 449 | mContactInfoSource = contactInfoSource; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 450 | mConversationCallbacks = convCallbacks; |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 451 | mSuperCollapsedListener = scbListener; |
Andy Huang | 65fe28f | 2012-04-06 18:08:53 -0700 | [diff] [blame] | 452 | mAddressCache = addressCache; |
Paul Westbrook | 8081df4 | 2012-09-10 15:43:36 -0700 | [diff] [blame] | 453 | mInflater = LayoutInflater.from(mContext); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 454 | |
| 455 | mItems = Lists.newArrayList(); |
Vikram Aggarwal | 69a6cdf | 2013-01-08 16:05:17 -0800 | [diff] [blame] | 456 | mMatcher = controllableActivity.getAccountController().getVeiledAddressMatcher(); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public int getCount() { |
| 461 | return mItems.size(); |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public int getItemViewType(int position) { |
| 466 | return mItems.get(position).getType(); |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public int getViewTypeCount() { |
| 471 | return VIEW_TYPE_COUNT; |
| 472 | } |
| 473 | |
| 474 | @Override |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 475 | public ConversationOverlayItem getItem(int position) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 476 | return mItems.get(position); |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public long getItemId(int position) { |
| 481 | return position; // TODO: ensure this works well enough |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public View getView(int position, View convertView, ViewGroup parent) { |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 486 | return getView(getItem(position), convertView, parent, false /* measureOnly */); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 489 | public View getView(ConversationOverlayItem item, View convertView, ViewGroup parent, |
| 490 | boolean measureOnly) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 491 | final View v; |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 492 | |
| 493 | if (convertView == null) { |
| 494 | v = item.createView(mContext, mInflater, parent); |
| 495 | } else { |
| 496 | v = convertView; |
| 497 | } |
Andy Huang | b8331b4 | 2012-07-16 19:08:53 -0700 | [diff] [blame] | 498 | item.bindView(v, measureOnly); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 499 | |
| 500 | return v; |
| 501 | } |
| 502 | |
Andrew Sapperstein | 888388c | 2013-08-01 22:51:37 -0700 | [diff] [blame^] | 503 | public LayoutInflater getLayoutInflater() { |
| 504 | return mInflater; |
| 505 | } |
| 506 | |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 507 | public FormattedDateBuilder getDateBuilder() { |
| 508 | return mDateBuilder; |
| 509 | } |
| 510 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 511 | public int addItem(ConversationOverlayItem item) { |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 512 | final int pos = mItems.size(); |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 513 | item.setPosition(pos); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 514 | mItems.add(item); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 515 | return pos; |
| 516 | } |
| 517 | |
| 518 | public void clear() { |
| 519 | mItems.clear(); |
Andy Huang | 6cb004b | 2012-10-26 15:37:32 -0700 | [diff] [blame] | 520 | notifyDataSetChanged(); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | public int addConversationHeader(Conversation conv) { |
| 524 | return addItem(new ConversationHeaderItem(conv)); |
| 525 | } |
| 526 | |
Scott Kennedy | 2027384 | 2012-11-07 11:16:21 -0800 | [diff] [blame] | 527 | public int addMessageHeader(ConversationMessage msg, boolean expanded, boolean showImages) { |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 528 | return addItem(new MessageHeaderItem(this, mDateBuilder, msg, expanded, showImages)); |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | public int addMessageFooter(MessageHeaderItem headerItem) { |
| 532 | return addItem(new MessageFooterItem(headerItem)); |
| 533 | } |
| 534 | |
Andrew Sapperstein | 4ddda2f | 2013-06-10 11:15:38 -0700 | [diff] [blame] | 535 | public static MessageHeaderItem newMessageHeaderItem(ConversationViewAdapter adapter, |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 536 | FormattedDateBuilder dateBuilder, ConversationMessage message, |
| 537 | boolean expanded, boolean showImages) { |
| 538 | return new MessageHeaderItem(adapter, dateBuilder, message, expanded, showImages); |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | public MessageFooterItem newMessageFooterItem(MessageHeaderItem headerItem) { |
| 542 | return new MessageFooterItem(headerItem); |
| 543 | } |
| 544 | |
| 545 | public int addSuperCollapsedBlock(int start, int end) { |
| 546 | return addItem(new SuperCollapsedBlockItem(start, end)); |
| 547 | } |
| 548 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 549 | public int addBorder( |
| 550 | boolean contiguous, boolean expanded, boolean firstBorder, boolean lastBorder) { |
| 551 | return addItem(new BorderItem(contiguous, expanded, firstBorder, lastBorder)); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 554 | public BorderItem newBorderItem(boolean contiguous, boolean expanded) { |
| 555 | return new BorderItem( |
| 556 | contiguous, expanded, false /* firstBorder */, false /* lastBorder */); |
Andrew Sapperstein | 14f9374 | 2013-07-25 14:29:56 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 559 | public void replaceSuperCollapsedBlock(SuperCollapsedBlockItem blockToRemove, |
| 560 | Collection<ConversationOverlayItem> replacements) { |
| 561 | final int pos = mItems.indexOf(blockToRemove); |
| 562 | if (pos == -1) { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | mItems.remove(pos); |
| 567 | mItems.addAll(pos, replacements); |
Andrew Sapperstein | cee3c90 | 2013-07-31 10:52:02 -0700 | [diff] [blame] | 568 | |
| 569 | // update position for all items |
| 570 | for (int i = 0, size = mItems.size(); i < size; i++) { |
| 571 | mItems.get(i).setPosition(i); |
| 572 | } |
Andy Huang | 46dfba6 | 2012-04-19 01:47:32 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 575 | public void updateItemsForMessage(ConversationMessage message, |
| 576 | List<Integer> affectedPositions) { |
| 577 | for (int i = 0, len = mItems.size(); i < len; i++) { |
| 578 | final ConversationOverlayItem item = mItems.get(i); |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 579 | if (item.belongsToMessage(message)) { |
| 580 | item.setMessage(message); |
Andy Huang | 6b3d0d9 | 2012-10-30 15:46:48 -0700 | [diff] [blame] | 581 | affectedPositions.add(i); |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 582 | } |
| 583 | } |
Andy Huang | 014ea4c | 2012-09-25 14:50:54 -0700 | [diff] [blame] | 584 | } |
Andy Huang | 7bdc375 | 2012-03-25 17:18:19 -0700 | [diff] [blame] | 585 | } |