blob: 403c643f13ee674f6853bc04c63e1a6093bf99a4 [file] [log] [blame]
Jim Miller9f0f0e02011-05-17 20:06:29 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.recent;
18
Jim Miller9f0f0e02011-05-17 20:06:29 -070019import android.animation.LayoutTransition;
Jim Miller9f0f0e02011-05-17 20:06:29 -070020import android.content.Context;
Jim Miller3c728fe2011-07-07 20:53:52 -070021import android.content.res.Configuration;
Jim Miller9f0f0e02011-05-17 20:06:29 -070022import android.database.DataSetObserver;
Michael Jurka3cd0a592011-08-16 12:40:30 -070023import android.graphics.Canvas;
Michael Jurka31a6fb32013-03-20 16:44:13 +010024import android.graphics.Paint;
Jim Miller9f0f0e02011-05-17 20:06:29 -070025import android.util.AttributeSet;
Michael Jurka99a96552012-01-27 17:23:38 -080026import android.util.DisplayMetrics;
27import android.util.FloatMath;
Jim Miller9f0f0e02011-05-17 20:06:29 -070028import android.util.Log;
Jim Miller9f0f0e02011-05-17 20:06:29 -070029import android.view.MotionEvent;
Jim Miller9f0f0e02011-05-17 20:06:29 -070030import android.view.View;
Jim Miller3c728fe2011-07-07 20:53:52 -070031import android.view.ViewConfiguration;
Michael Jurka99a96552012-01-27 17:23:38 -080032import android.view.ViewTreeObserver;
33import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jim Miller9f0f0e02011-05-17 20:06:29 -070034import android.widget.LinearLayout;
35import android.widget.ScrollView;
36
37import com.android.systemui.R;
Michael Jurka07d40462011-07-19 10:54:38 -070038import com.android.systemui.SwipeHelper;
Michael Jurkaab48b682011-09-12 15:39:45 -070039import com.android.systemui.recent.RecentsPanelView.TaskDescriptionAdapter;
Jim Miller9f0f0e02011-05-17 20:06:29 -070040
Michael Jurkad1a040c2012-06-06 10:05:46 -070041import java.util.HashSet;
42import java.util.Iterator;
Michael Jurka99a96552012-01-27 17:23:38 -080043
Michael Jurka4eaa9832012-02-29 15:51:49 -080044public class RecentsVerticalScrollView extends ScrollView
45 implements SwipeHelper.Callback, RecentsPanelView.RecentsScrollView {
Jim Miller9f0f0e02011-05-17 20:06:29 -070046 private static final String TAG = RecentsPanelView.TAG;
Jim Miller3c728fe2011-07-07 20:53:52 -070047 private static final boolean DEBUG = RecentsPanelView.DEBUG;
Jim Miller9f0f0e02011-05-17 20:06:29 -070048 private LinearLayout mLinearLayout;
Michael Jurkaab48b682011-09-12 15:39:45 -070049 private TaskDescriptionAdapter mAdapter;
Jim Miller9f0f0e02011-05-17 20:06:29 -070050 private RecentsCallback mCallback;
51 protected int mLastScrollPosition;
Michael Jurka07d40462011-07-19 10:54:38 -070052 private SwipeHelper mSwipeHelper;
Michael Jurka3cd0a592011-08-16 12:40:30 -070053 private RecentsScrollViewPerformanceHelper mPerformanceHelper;
Michael Jurkad1a040c2012-06-06 10:05:46 -070054 private HashSet<View> mRecycledViews;
Michael Jurka99a96552012-01-27 17:23:38 -080055 private int mNumItemsInOneScreenful;
Michael Jurka07d40462011-07-19 10:54:38 -070056
Jim Miller9f0f0e02011-05-17 20:06:29 -070057 public RecentsVerticalScrollView(Context context, AttributeSet attrs) {
58 super(context, attrs, 0);
Michael Jurka07d40462011-07-19 10:54:38 -070059 float densityScale = getResources().getDisplayMetrics().density;
60 float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop();
61 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
Michael Jurka3cd0a592011-08-16 12:40:30 -070062
63 mPerformanceHelper = RecentsScrollViewPerformanceHelper.create(context, attrs, this, true);
Michael Jurkad1a040c2012-06-06 10:05:46 -070064 mRecycledViews = new HashSet<View>();
Jim Miller9f0f0e02011-05-17 20:06:29 -070065 }
66
Michael Jurka4eaa9832012-02-29 15:51:49 -080067 public void setMinSwipeAlpha(float minAlpha) {
68 mSwipeHelper.setMinAlpha(minAlpha);
69 }
70
Jim Miller9f0f0e02011-05-17 20:06:29 -070071 private int scrollPositionOfMostRecent() {
Michael Jurka31a6fb32013-03-20 16:44:13 +010072 return mLinearLayout.getHeight() - getHeight() + mPaddingTop;
Jim Miller9f0f0e02011-05-17 20:06:29 -070073 }
74
Michael Jurka261277e2012-06-01 04:06:45 -070075 private void addToRecycledViews(View v) {
76 if (mRecycledViews.size() < mNumItemsInOneScreenful) {
77 mRecycledViews.add(v);
78 }
79 }
80
Michael Jurkae5923632012-10-03 15:29:36 +020081 public View findViewForTask(int persistentTaskId) {
Michael Jurkacb2522c2012-04-13 09:32:47 -070082 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
83 View v = mLinearLayout.getChildAt(i);
84 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) v.getTag();
Michael Jurkae5923632012-10-03 15:29:36 +020085 if (holder.taskDescription.persistentTaskId == persistentTaskId) {
Michael Jurkacb2522c2012-04-13 09:32:47 -070086 return v;
87 }
88 }
89 return null;
90 }
91
Jim Millerc0d27312011-07-14 18:54:01 -070092 private void update() {
Michael Jurka99a96552012-01-27 17:23:38 -080093 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
94 View v = mLinearLayout.getChildAt(i);
Michael Jurka261277e2012-06-01 04:06:45 -070095 addToRecycledViews(v);
Michael Jurka99a96552012-01-27 17:23:38 -080096 mAdapter.recycleView(v);
97 }
98 LayoutTransition transitioner = getLayoutTransition();
99 setLayoutTransition(null);
100
Jim Miller9f0f0e02011-05-17 20:06:29 -0700101 mLinearLayout.removeAllViews();
Michael Jurkad1a040c2012-06-06 10:05:46 -0700102
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700103 // Once we can clear the data associated with individual item views,
104 // we can get rid of the removeAllViews() and the code below will
105 // recycle them.
Michael Jurkad1a040c2012-06-06 10:05:46 -0700106 Iterator<View> recycledViews = mRecycledViews.iterator();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700107 for (int i = 0; i < mAdapter.getCount(); i++) {
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700108 View old = null;
Michael Jurkad1a040c2012-06-06 10:05:46 -0700109 if (recycledViews.hasNext()) {
110 old = recycledViews.next();
111 recycledViews.remove();
Michael Jurka99a96552012-01-27 17:23:38 -0800112 old.setVisibility(VISIBLE);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700113 }
114 final View view = mAdapter.getView(i, old, mLinearLayout);
Michael Jurka07d40462011-07-19 10:54:38 -0700115
Michael Jurka3cd0a592011-08-16 12:40:30 -0700116 if (mPerformanceHelper != null) {
117 mPerformanceHelper.addViewCallback(view);
118 }
119
Michael Jurka99a96552012-01-27 17:23:38 -0800120 OnTouchListener noOpListener = new OnTouchListener() {
121 @Override
122 public boolean onTouch(View v, MotionEvent event) {
123 return true;
124 }
125 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700126
Michael Jurka99a96552012-01-27 17:23:38 -0800127 view.setOnClickListener(new OnClickListener() {
128 public void onClick(View v) {
129 mCallback.dismiss();
130 }
131 });
132 // We don't want a click sound when we dimiss recents
133 view.setSoundEffectsEnabled(false);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700134
Michael Jurka99a96552012-01-27 17:23:38 -0800135 OnClickListener launchAppListener = new OnClickListener() {
136 public void onClick(View v) {
137 mCallback.handleOnClick(view);
138 }
139 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700140
Michael Jurka99a96552012-01-27 17:23:38 -0800141 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) view.getTag();
142 final View thumbnailView = holder.thumbnailView;
143 OnLongClickListener longClickListener = new OnLongClickListener() {
144 public boolean onLongClick(View v) {
145 final View anchorView = view.findViewById(R.id.app_description);
146 mCallback.handleLongPress(view, anchorView, thumbnailView);
147 return true;
148 }
149 };
150 thumbnailView.setClickable(true);
151 thumbnailView.setOnClickListener(launchAppListener);
152 thumbnailView.setOnLongClickListener(longClickListener);
Michael Jurka7daf95d2011-09-30 11:07:30 -0700153
Michael Jurka99a96552012-01-27 17:23:38 -0800154 // We don't want to dismiss recents if a user clicks on the app title
155 // (we also don't want to launch the app either, though, because the
156 // app title is a small target and doesn't have great click feedback)
157 final View appTitle = view.findViewById(R.id.app_label);
158 appTitle.setContentDescription(" ");
159 appTitle.setOnTouchListener(noOpListener);
160 final View calloutLine = view.findViewById(R.id.recents_callout_line);
Michael Jurkacb2522c2012-04-13 09:32:47 -0700161 if (calloutLine != null) {
162 calloutLine.setOnTouchListener(noOpListener);
163 }
Michael Jurka99a96552012-01-27 17:23:38 -0800164
165 mLinearLayout.addView(view);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700166 }
Michael Jurka99a96552012-01-27 17:23:38 -0800167 setLayoutTransition(transitioner);
168
Michael Jurka9a0890c2012-10-02 14:42:53 +0200169 // Scroll to end after initial layout.
Michael Jurka99a96552012-01-27 17:23:38 -0800170 final OnGlobalLayoutListener updateScroll = new OnGlobalLayoutListener() {
171 public void onGlobalLayout() {
172 mLastScrollPosition = scrollPositionOfMostRecent();
173 scrollTo(0, mLastScrollPosition);
Michael Jurka9a0890c2012-10-02 14:42:53 +0200174 final ViewTreeObserver observer = getViewTreeObserver();
Michael Jurka99a96552012-01-27 17:23:38 -0800175 if (observer.isAlive()) {
176 observer.removeOnGlobalLayoutListener(this);
177 }
178 }
179 };
Michael Jurka9a0890c2012-10-02 14:42:53 +0200180 getViewTreeObserver().addOnGlobalLayoutListener(updateScroll);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700181 }
182
183 @Override
Jim Millerc0d27312011-07-14 18:54:01 -0700184 public void removeViewInLayout(final View view) {
Michael Jurka07d40462011-07-19 10:54:38 -0700185 dismissChild(view);
Jim Millerc0d27312011-07-14 18:54:01 -0700186 }
187
Jim Miller9f0f0e02011-05-17 20:06:29 -0700188 public boolean onInterceptTouchEvent(MotionEvent ev) {
Jim Millerc0d27312011-07-14 18:54:01 -0700189 if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
Michael Jurka07d40462011-07-19 10:54:38 -0700190 return mSwipeHelper.onInterceptTouchEvent(ev) ||
191 super.onInterceptTouchEvent(ev);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700192 }
193
194 @Override
195 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurka07d40462011-07-19 10:54:38 -0700196 return mSwipeHelper.onTouchEvent(ev) ||
197 super.onTouchEvent(ev);
198 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700199
Michael Jurka07d40462011-07-19 10:54:38 -0700200 public boolean canChildBeDismissed(View v) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700201 return true;
202 }
203
Michael Jurka07d40462011-07-19 10:54:38 -0700204 public void dismissChild(View v) {
205 mSwipeHelper.dismissChild(v, 0);
Jim Millerc0d27312011-07-14 18:54:01 -0700206 }
207
Michael Jurka07d40462011-07-19 10:54:38 -0700208 public void onChildDismissed(View v) {
Michael Jurka261277e2012-06-01 04:06:45 -0700209 addToRecycledViews(v);
Michael Jurka07d40462011-07-19 10:54:38 -0700210 mLinearLayout.removeView(v);
211 mCallback.handleSwipe(v);
Michael Jurka2db72fc2012-02-23 17:16:11 -0800212 // Restore the alpha/translation parameters to what they were before swiping
213 // (for when these items are recycled)
214 View contentView = getChildContentView(v);
215 contentView.setAlpha(1f);
216 contentView.setTranslationX(0);
Michael Jurka07d40462011-07-19 10:54:38 -0700217 }
218
219 public void onBeginDrag(View v) {
Michael Jurka13451a42011-08-22 15:54:21 -0700220 // We do this so the underlying ScrollView knows that it won't get
221 // the chance to intercept events anymore
222 requestDisallowInterceptTouchEvent(true);
Peter Ng622a9762011-08-29 10:56:53 -0700223 }
224
225 public void onDragCancelled(View v) {
Michael Jurka07d40462011-07-19 10:54:38 -0700226 }
227
228 public View getChildAtPosition(MotionEvent ev) {
229 final float x = ev.getX() + getScrollX();
230 final float y = ev.getY() + getScrollY();
231 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
232 View item = mLinearLayout.getChildAt(i);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700233 if (item.getVisibility() == View.VISIBLE
234 && x >= item.getLeft() && x < item.getRight()
235 && y >= item.getTop() && y < item.getBottom()) {
Michael Jurka07d40462011-07-19 10:54:38 -0700236 return item;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700237 }
238 }
Michael Jurka07d40462011-07-19 10:54:38 -0700239 return null;
240 }
241
242 public View getChildContentView(View v) {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700243 return v.findViewById(R.id.recent_item);
244 }
245
246 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700247 public void draw(Canvas canvas) {
248 super.draw(canvas);
249
250 if (mPerformanceHelper != null) {
251 int paddingLeft = mPaddingLeft;
252 final boolean offsetRequired = isPaddingOffsetRequired();
253 if (offsetRequired) {
254 paddingLeft += getLeftPaddingOffset();
255 }
256
257 int left = mScrollX + paddingLeft;
258 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
259 int top = mScrollY + getFadeTop(offsetRequired);
260 int bottom = top + getFadeHeight(offsetRequired);
261
262 if (offsetRequired) {
263 right += getRightPaddingOffset();
264 bottom += getBottomPaddingOffset();
265 }
266 mPerformanceHelper.drawCallback(canvas,
267 left, right, top, bottom, mScrollX, mScrollY,
268 getTopFadingEdgeStrength(), getBottomFadingEdgeStrength(),
Michael Jurka31a6fb32013-03-20 16:44:13 +0100269 0, 0, mPaddingTop);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700270 }
271 }
272
273 @Override
274 public int getVerticalFadingEdgeLength() {
275 if (mPerformanceHelper != null) {
276 return mPerformanceHelper.getVerticalFadingEdgeLengthCallback();
277 } else {
278 return super.getVerticalFadingEdgeLength();
279 }
280 }
281
282 @Override
283 public int getHorizontalFadingEdgeLength() {
284 if (mPerformanceHelper != null) {
285 return mPerformanceHelper.getHorizontalFadingEdgeLengthCallback();
286 } else {
287 return super.getHorizontalFadingEdgeLength();
288 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700289 }
290
291 @Override
292 protected void onFinishInflate() {
293 super.onFinishInflate();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700294 setScrollbarFadingEnabled(true);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700295 mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700296 final int leftPadding = mContext.getResources()
297 .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin);
298 setOverScrollEffectPadding(leftPadding, 0);
299 }
300
Jim Miller3c728fe2011-07-07 20:53:52 -0700301 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700302 public void onAttachedToWindow() {
303 if (mPerformanceHelper != null) {
304 mPerformanceHelper.onAttachedToWindowCallback(
305 mCallback, mLinearLayout, isHardwareAccelerated());
306 }
307 }
308
309 @Override
Jim Miller3c728fe2011-07-07 20:53:52 -0700310 protected void onConfigurationChanged(Configuration newConfig) {
311 super.onConfigurationChanged(newConfig);
Michael Jurka07d40462011-07-19 10:54:38 -0700312 float densityScale = getResources().getDisplayMetrics().density;
313 mSwipeHelper.setDensityScale(densityScale);
314 float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop();
315 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
Jim Miller3c728fe2011-07-07 20:53:52 -0700316 }
317
Jim Miller9f0f0e02011-05-17 20:06:29 -0700318 private void setOverScrollEffectPadding(int leftPadding, int i) {
319 // TODO Add to (Vertical)ScrollView
320 }
321
322 @Override
323 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
324 super.onSizeChanged(w, h, oldw, oldh);
Chet Haase81abe872011-08-10 13:53:43 -0700325
326 // Skip this work if a transition is running; it sets the scroll values independently
327 // and should not have those animated values clobbered by this logic
328 LayoutTransition transition = mLinearLayout.getLayoutTransition();
329 if (transition != null && transition.isRunning()) {
330 return;
331 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700332 // Keep track of the last visible item in the list so we can restore it
333 // to the bottom when the orientation changes.
334 mLastScrollPosition = scrollPositionOfMostRecent();
335
336 // This has to happen post-layout, so run it "in the future"
337 post(new Runnable() {
338 public void run() {
Chet Haase81abe872011-08-10 13:53:43 -0700339 // Make sure we're still not clobbering the transition-set values, since this
340 // runnable launches asynchronously
341 LayoutTransition transition = mLinearLayout.getLayoutTransition();
342 if (transition == null || !transition.isRunning()) {
343 scrollTo(0, mLastScrollPosition);
344 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700345 }
346 });
347 }
348
Michael Jurkaab48b682011-09-12 15:39:45 -0700349 public void setAdapter(TaskDescriptionAdapter adapter) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700350 mAdapter = adapter;
351 mAdapter.registerDataSetObserver(new DataSetObserver() {
352 public void onChanged() {
353 update();
354 }
355
356 public void onInvalidated() {
357 update();
358 }
359 });
Michael Jurka99a96552012-01-27 17:23:38 -0800360
361 DisplayMetrics dm = getResources().getDisplayMetrics();
362 int childWidthMeasureSpec =
363 MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST);
364 int childheightMeasureSpec =
365 MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST);
366 View child = mAdapter.createView(mLinearLayout);
367 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
368 mNumItemsInOneScreenful =
369 (int) FloatMath.ceil(dm.heightPixels / (float) child.getMeasuredHeight());
Michael Jurka261277e2012-06-01 04:06:45 -0700370 addToRecycledViews(child);
Michael Jurka99a96552012-01-27 17:23:38 -0800371
372 for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) {
Michael Jurka261277e2012-06-01 04:06:45 -0700373 addToRecycledViews(mAdapter.createView(mLinearLayout));
Michael Jurka99a96552012-01-27 17:23:38 -0800374 }
375 }
376
377 public int numItemsInOneScreenful() {
378 return mNumItemsInOneScreenful;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700379 }
380
381 @Override
382 public void setLayoutTransition(LayoutTransition transition) {
383 // The layout transition applies to our embedded LinearLayout
384 mLinearLayout.setLayoutTransition(transition);
385 }
386
Jim Miller9f0f0e02011-05-17 20:06:29 -0700387 public void setCallback(RecentsCallback callback) {
388 mCallback = callback;
389 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700390}