1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mail.ui;
import android.animation.ObjectAnimator;
import android.app.LoaderManager;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.mail.R;
import com.android.mail.browse.ConversationCursor;
import com.android.mail.providers.Folder;
import com.android.mail.utils.LogTag;
/**
* Base class to display tip teasers in the thread list.
* Supports two-line text and start/end icons.
*/
public abstract class ConversationTipView extends LinearLayout
implements ConversationSpecialItemView, SwipeableItemView, View.OnClickListener {
protected static final String LOG_TAG = LogTag.getLogTag();
protected Context mContext;
protected AnimatedAdapter mAdapter;
private int mScrollSlop;
private int mShrinkAnimationDuration;
private int mAnimatedHeight = -1;
protected View mSwipeableContent;
private View mContent;
private TextView mText;
public ConversationTipView(Context context) {
this(context, null);
}
public ConversationTipView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
final Resources resources = context.getResources();
mScrollSlop = resources.getInteger(R.integer.swipeScrollSlop);
mShrinkAnimationDuration = resources.getInteger(
R.integer.shrink_animation_duration);
// Inflate the actual content and add it to this view
mContent = LayoutInflater.from(mContext).inflate(getChildLayout(), this, false);
addView(mContent);
setupViews();
}
@Override
public ViewGroup.LayoutParams getLayoutParams() {
ViewGroup.LayoutParams params = super.getLayoutParams();
if (params != null) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
return params;
}
protected @LayoutRes int getChildLayout() {
// Should override setupViews as well if this is overridden.
return R.layout.conversation_tip_view;
}
protected void setupViews() {
// If this is overridden, child classes cannot rely on setText/getStartIconAttr/etc.
mSwipeableContent = mContent.findViewById(R.id.conversation_tip_swipeable_content);
mText = (TextView) mContent.findViewById(R.id.conversation_tip_text);
final ImageView startImage = (ImageView) mContent.findViewById(R.id.conversation_tip_icon1);
final ImageView dismiss = (ImageView) mContent.findViewById(R.id.dismiss_icon);
// Bind content (text content must be bound by calling setText(..))
bindIcon(startImage, getStartIconAttr());
// Bind listeners
dismiss.setOnClickListener(this);
mText.setOnClickListener(getTextAreaOnClickListener());
}
/**
* Helper function to bind the additional attributes to the icon, or make the icon GONE.
*/
private void bindIcon(ImageView image, ImageAttrSet attr) {
if (attr != null) {
image.setVisibility(VISIBLE);
image.setContentDescription(attr.contentDescription);
// Must override resId for the actual icon, so no need to check -1 here.
image.setImageResource(attr.resId);
if (attr.background != -1) {
image.setBackgroundResource(attr.background);
}
} else {
image.setVisibility(GONE);
}
}
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
if (mAnimatedHeight == -1) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
setMeasuredDimension(View.MeasureSpec.getSize(widthMeasureSpec), mAnimatedHeight);
}
}
protected ImageAttrSet getStartIconAttr() {
return null;
}
protected void setText(CharSequence text) {
mText.setText(text);
}
protected OnClickListener getTextAreaOnClickListener() {
return null;
}
@Override
public void onClick(View view) {
// Default on click for the default dismiss button
dismiss();
}
@Override
public void onUpdate(Folder folder, ConversationCursor cursor) {
// Do nothing by default
}
@Override
public void onGetView() {
// Do nothing by default
}
@Override
public int getPosition() {
// By default the tip teasers go on top of the list.
return 0;
}
@Override
public void setAdapter(AnimatedAdapter adapter) {
mAdapter = adapter;
}
@Override
public void bindFragment(LoaderManager loaderManager, Bundle savedInstanceState) {
// Do nothing by default
}
@Override
public void cleanup() {
// Do nothing by default
}
@Override
public void onConversationSelected() {
// Do nothing by default
}
@Override
public void onCabModeEntered() {
// Do nothing by default
}
@Override
public void onCabModeExited() {
// Do nothing by default
}
@Override
public boolean acceptsUserTaps() {
return true;
}
@Override
public void onConversationListVisibilityChanged(boolean visible) {
// Do nothing by default
}
@Override
public void saveInstanceState(Bundle outState) {
// Do nothing by default
}
@Override
public boolean commitLeaveBehindItem() {
// Tip has no leave-behind by default
return false;
}
@Override
public SwipeableView getSwipeableView() {
return SwipeableView.from(mSwipeableContent);
}
@Override
public boolean canChildBeDismissed() {
return true;
}
@Override
public void dismiss() {
startDestroyAnimation();
}
@Override
public float getMinAllowScrollDistance() {
return mScrollSlop;
}
private void startDestroyAnimation() {
final int start = getHeight();
final int end = 0;
mAnimatedHeight = start;
final ObjectAnimator heightAnimator =
ObjectAnimator.ofInt(this, "animatedHeight", start, end);
heightAnimator.setInterpolator(new DecelerateInterpolator(2.0f));
heightAnimator.setDuration(mShrinkAnimationDuration);
heightAnimator.start();
/*
* Ideally, we would like to call mAdapter.notifyDataSetChanged() in a listener's
* onAnimationEnd(), but we are in the middle of a touch event, and this will cause all the
* views to get recycled, which will cause problems.
*
* Instead, we'll just leave the item in the list with a height of 0, and the next
* notifyDatasetChanged() will remove it from the adapter.
*/
}
/**
* This method is used by the animator. It is explicitly kept in proguard.flags to prevent it
* from being removed, inlined, or obfuscated.
* Edit ./vendor/unbundled/packages/apps/UnifiedGmail/proguard.flags
* In the future, we want to use @Keep
*/
public void setAnimatedHeight(final int height) {
mAnimatedHeight = height;
requestLayout();
}
public static class ImageAttrSet {
// -1 for these resIds to not override the default value.
public int resId;
public int background;
public String contentDescription;
public ImageAttrSet(int resId, int background, String contentDescription) {
this.resId = resId;
this.background = background;
this.contentDescription = contentDescription;
}
}
}
|