blob: 1e247be0268e0775c06c59d3b8a769e2a838efa3 [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;
Jim Miller9f0f0e02011-05-17 20:06:29 -070024import android.util.AttributeSet;
Michael Jurka99a96552012-01-27 17:23:38 -080025import android.util.DisplayMetrics;
26import android.util.FloatMath;
Jim Miller9f0f0e02011-05-17 20:06:29 -070027import android.util.Log;
Jim Miller9f0f0e02011-05-17 20:06:29 -070028import android.view.MotionEvent;
Jim Miller9f0f0e02011-05-17 20:06:29 -070029import android.view.View;
Jim Miller3c728fe2011-07-07 20:53:52 -070030import android.view.ViewConfiguration;
Michael Jurka99a96552012-01-27 17:23:38 -080031import android.view.ViewTreeObserver;
32import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jim Miller9f0f0e02011-05-17 20:06:29 -070033import android.widget.LinearLayout;
34import android.widget.ScrollView;
35
36import com.android.systemui.R;
Michael Jurka07d40462011-07-19 10:54:38 -070037import com.android.systemui.SwipeHelper;
Michael Jurkaab48b682011-09-12 15:39:45 -070038import com.android.systemui.recent.RecentsPanelView.TaskDescriptionAdapter;
Jim Miller9f0f0e02011-05-17 20:06:29 -070039
Michael Jurkad1a040c2012-06-06 10:05:46 -070040import java.util.HashSet;
41import java.util.Iterator;
Michael Jurka99a96552012-01-27 17:23:38 -080042
Michael Jurka4eaa9832012-02-29 15:51:49 -080043public class RecentsVerticalScrollView extends ScrollView
44 implements SwipeHelper.Callback, RecentsPanelView.RecentsScrollView {
Jim Miller9f0f0e02011-05-17 20:06:29 -070045 private static final String TAG = RecentsPanelView.TAG;
Jim Miller3c728fe2011-07-07 20:53:52 -070046 private static final boolean DEBUG = RecentsPanelView.DEBUG;
Jim Miller9f0f0e02011-05-17 20:06:29 -070047 private LinearLayout mLinearLayout;
Michael Jurkaab48b682011-09-12 15:39:45 -070048 private TaskDescriptionAdapter mAdapter;
Jim Miller9f0f0e02011-05-17 20:06:29 -070049 private RecentsCallback mCallback;
50 protected int mLastScrollPosition;
Michael Jurka07d40462011-07-19 10:54:38 -070051 private SwipeHelper mSwipeHelper;
Michael Jurka4a6bc552013-09-17 17:42:54 +020052 private FadedEdgeDrawHelper mFadedEdgeDrawHelper;
Michael Jurkad1a040c2012-06-06 10:05:46 -070053 private HashSet<View> mRecycledViews;
Michael Jurka99a96552012-01-27 17:23:38 -080054 private int mNumItemsInOneScreenful;
Michael Jurka4a6bc552013-09-17 17:42:54 +020055 private Runnable mOnScrollListener;
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);
Dan Sandlereceda3d2014-07-21 15:35:01 -040059 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, context);
Michael Jurka3cd0a592011-08-16 12:40:30 -070060
Michael Jurka4a6bc552013-09-17 17:42:54 +020061 mFadedEdgeDrawHelper = FadedEdgeDrawHelper.create(context, attrs, this, true);
Michael Jurkad1a040c2012-06-06 10:05:46 -070062 mRecycledViews = new HashSet<View>();
Jim Miller9f0f0e02011-05-17 20:06:29 -070063 }
64
Michael Jurka4eaa9832012-02-29 15:51:49 -080065 public void setMinSwipeAlpha(float minAlpha) {
Adrian Roos5d9cc662014-05-28 17:08:13 +020066 mSwipeHelper.setMinSwipeProgress(minAlpha);
Michael Jurka4eaa9832012-02-29 15:51:49 -080067 }
68
Jim Miller9f0f0e02011-05-17 20:06:29 -070069 private int scrollPositionOfMostRecent() {
John Spurlock01534782014-01-13 11:59:22 -050070 return mLinearLayout.getHeight() - getHeight() + getPaddingTop();
Jim Miller9f0f0e02011-05-17 20:06:29 -070071 }
72
Michael Jurka261277e2012-06-01 04:06:45 -070073 private void addToRecycledViews(View v) {
74 if (mRecycledViews.size() < mNumItemsInOneScreenful) {
75 mRecycledViews.add(v);
76 }
77 }
78
Michael Jurkae5923632012-10-03 15:29:36 +020079 public View findViewForTask(int persistentTaskId) {
Michael Jurkacb2522c2012-04-13 09:32:47 -070080 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
81 View v = mLinearLayout.getChildAt(i);
82 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) v.getTag();
Michael Jurkae5923632012-10-03 15:29:36 +020083 if (holder.taskDescription.persistentTaskId == persistentTaskId) {
Michael Jurkacb2522c2012-04-13 09:32:47 -070084 return v;
85 }
86 }
87 return null;
88 }
89
Jim Millerc0d27312011-07-14 18:54:01 -070090 private void update() {
Michael Jurka99a96552012-01-27 17:23:38 -080091 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
92 View v = mLinearLayout.getChildAt(i);
Michael Jurka261277e2012-06-01 04:06:45 -070093 addToRecycledViews(v);
Michael Jurka99a96552012-01-27 17:23:38 -080094 mAdapter.recycleView(v);
95 }
96 LayoutTransition transitioner = getLayoutTransition();
97 setLayoutTransition(null);
98
Jim Miller9f0f0e02011-05-17 20:06:29 -070099 mLinearLayout.removeAllViews();
Michael Jurkad1a040c2012-06-06 10:05:46 -0700100
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700101 // Once we can clear the data associated with individual item views,
102 // we can get rid of the removeAllViews() and the code below will
103 // recycle them.
Michael Jurkad1a040c2012-06-06 10:05:46 -0700104 Iterator<View> recycledViews = mRecycledViews.iterator();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700105 for (int i = 0; i < mAdapter.getCount(); i++) {
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700106 View old = null;
Michael Jurkad1a040c2012-06-06 10:05:46 -0700107 if (recycledViews.hasNext()) {
108 old = recycledViews.next();
109 recycledViews.remove();
Michael Jurka99a96552012-01-27 17:23:38 -0800110 old.setVisibility(VISIBLE);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700111 }
112 final View view = mAdapter.getView(i, old, mLinearLayout);
Michael Jurka07d40462011-07-19 10:54:38 -0700113
Michael Jurka4a6bc552013-09-17 17:42:54 +0200114 if (mFadedEdgeDrawHelper != null) {
115 mFadedEdgeDrawHelper.addViewCallback(view);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700116 }
117
Michael Jurka99a96552012-01-27 17:23:38 -0800118 OnTouchListener noOpListener = new OnTouchListener() {
119 @Override
120 public boolean onTouch(View v, MotionEvent event) {
121 return true;
122 }
123 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700124
Michael Jurka99a96552012-01-27 17:23:38 -0800125 view.setOnClickListener(new OnClickListener() {
126 public void onClick(View v) {
127 mCallback.dismiss();
128 }
129 });
130 // We don't want a click sound when we dimiss recents
131 view.setSoundEffectsEnabled(false);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700132
Michael Jurka99a96552012-01-27 17:23:38 -0800133 OnClickListener launchAppListener = new OnClickListener() {
134 public void onClick(View v) {
135 mCallback.handleOnClick(view);
136 }
137 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700138
Michael Jurka99a96552012-01-27 17:23:38 -0800139 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) view.getTag();
140 final View thumbnailView = holder.thumbnailView;
141 OnLongClickListener longClickListener = new OnLongClickListener() {
142 public boolean onLongClick(View v) {
143 final View anchorView = view.findViewById(R.id.app_description);
144 mCallback.handleLongPress(view, anchorView, thumbnailView);
145 return true;
146 }
147 };
148 thumbnailView.setClickable(true);
149 thumbnailView.setOnClickListener(launchAppListener);
150 thumbnailView.setOnLongClickListener(longClickListener);
Michael Jurka7daf95d2011-09-30 11:07:30 -0700151
Michael Jurka99a96552012-01-27 17:23:38 -0800152 // We don't want to dismiss recents if a user clicks on the app title
153 // (we also don't want to launch the app either, though, because the
154 // app title is a small target and doesn't have great click feedback)
155 final View appTitle = view.findViewById(R.id.app_label);
156 appTitle.setContentDescription(" ");
157 appTitle.setOnTouchListener(noOpListener);
158 final View calloutLine = view.findViewById(R.id.recents_callout_line);
Michael Jurkacb2522c2012-04-13 09:32:47 -0700159 if (calloutLine != null) {
160 calloutLine.setOnTouchListener(noOpListener);
161 }
Michael Jurka99a96552012-01-27 17:23:38 -0800162
163 mLinearLayout.addView(view);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700164 }
Michael Jurka99a96552012-01-27 17:23:38 -0800165 setLayoutTransition(transitioner);
166
Michael Jurka9a0890c2012-10-02 14:42:53 +0200167 // Scroll to end after initial layout.
Michael Jurka99a96552012-01-27 17:23:38 -0800168 final OnGlobalLayoutListener updateScroll = new OnGlobalLayoutListener() {
169 public void onGlobalLayout() {
170 mLastScrollPosition = scrollPositionOfMostRecent();
171 scrollTo(0, mLastScrollPosition);
Michael Jurka9a0890c2012-10-02 14:42:53 +0200172 final ViewTreeObserver observer = getViewTreeObserver();
Michael Jurka99a96552012-01-27 17:23:38 -0800173 if (observer.isAlive()) {
174 observer.removeOnGlobalLayoutListener(this);
175 }
176 }
177 };
Michael Jurka9a0890c2012-10-02 14:42:53 +0200178 getViewTreeObserver().addOnGlobalLayoutListener(updateScroll);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700179 }
180
181 @Override
Jim Millerc0d27312011-07-14 18:54:01 -0700182 public void removeViewInLayout(final View view) {
Michael Jurka07d40462011-07-19 10:54:38 -0700183 dismissChild(view);
Jim Millerc0d27312011-07-14 18:54:01 -0700184 }
185
Jim Miller9f0f0e02011-05-17 20:06:29 -0700186 public boolean onInterceptTouchEvent(MotionEvent ev) {
Jim Millerc0d27312011-07-14 18:54:01 -0700187 if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
Michael Jurka07d40462011-07-19 10:54:38 -0700188 return mSwipeHelper.onInterceptTouchEvent(ev) ||
189 super.onInterceptTouchEvent(ev);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700190 }
191
192 @Override
193 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurka07d40462011-07-19 10:54:38 -0700194 return mSwipeHelper.onTouchEvent(ev) ||
195 super.onTouchEvent(ev);
196 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700197
Michael Jurka07d40462011-07-19 10:54:38 -0700198 public boolean canChildBeDismissed(View v) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700199 return true;
200 }
201
Selim Cinek19c8c702014-08-25 22:09:19 +0200202 @Override
203 public boolean isAntiFalsingNeeded() {
204 return false;
205 }
206
Selim Cinek34cf5c42014-09-26 15:39:00 +0200207 @Override
208 public float getFalsingThresholdFactor() {
209 return 1.0f;
210 }
211
Michael Jurka07d40462011-07-19 10:54:38 -0700212 public void dismissChild(View v) {
213 mSwipeHelper.dismissChild(v, 0);
Jim Millerc0d27312011-07-14 18:54:01 -0700214 }
215
Michael Jurka07d40462011-07-19 10:54:38 -0700216 public void onChildDismissed(View v) {
Michael Jurka261277e2012-06-01 04:06:45 -0700217 addToRecycledViews(v);
Michael Jurka07d40462011-07-19 10:54:38 -0700218 mLinearLayout.removeView(v);
219 mCallback.handleSwipe(v);
Michael Jurka2db72fc2012-02-23 17:16:11 -0800220 // Restore the alpha/translation parameters to what they were before swiping
221 // (for when these items are recycled)
222 View contentView = getChildContentView(v);
223 contentView.setAlpha(1f);
224 contentView.setTranslationX(0);
Michael Jurka07d40462011-07-19 10:54:38 -0700225 }
226
227 public void onBeginDrag(View v) {
Michael Jurka13451a42011-08-22 15:54:21 -0700228 // We do this so the underlying ScrollView knows that it won't get
229 // the chance to intercept events anymore
230 requestDisallowInterceptTouchEvent(true);
Peter Ng622a9762011-08-29 10:56:53 -0700231 }
232
233 public void onDragCancelled(View v) {
Michael Jurka07d40462011-07-19 10:54:38 -0700234 }
235
Selim Cinekeb973562014-05-02 17:07:49 +0200236 @Override
237 public void onChildSnappedBack(View animView) {
238 }
239
Adrian Roos5d9cc662014-05-28 17:08:13 +0200240 @Override
241 public boolean updateSwipeProgress(View animView, boolean dismissable, float swipeProgress) {
242 return false;
243 }
244
Michael Jurka07d40462011-07-19 10:54:38 -0700245 public View getChildAtPosition(MotionEvent ev) {
246 final float x = ev.getX() + getScrollX();
247 final float y = ev.getY() + getScrollY();
248 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
249 View item = mLinearLayout.getChildAt(i);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700250 if (item.getVisibility() == View.VISIBLE
251 && x >= item.getLeft() && x < item.getRight()
252 && y >= item.getTop() && y < item.getBottom()) {
Michael Jurka07d40462011-07-19 10:54:38 -0700253 return item;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700254 }
255 }
Michael Jurka07d40462011-07-19 10:54:38 -0700256 return null;
257 }
258
259 public View getChildContentView(View v) {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700260 return v.findViewById(R.id.recent_item);
261 }
262
263 @Override
Michael Jurka4a6bc552013-09-17 17:42:54 +0200264 public void drawFadedEdges(Canvas canvas, int left, int right, int top, int bottom) {
265 if (mFadedEdgeDrawHelper != null) {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700266 final boolean offsetRequired = isPaddingOffsetRequired();
Michael Jurka4a6bc552013-09-17 17:42:54 +0200267 mFadedEdgeDrawHelper.drawCallback(canvas,
John Spurlock01534782014-01-13 11:59:22 -0500268 left, right, top + getFadeTop(offsetRequired), bottom, getScrollX(), getScrollY(),
Michael Jurka3cd0a592011-08-16 12:40:30 -0700269 getTopFadingEdgeStrength(), getBottomFadingEdgeStrength(),
John Spurlock01534782014-01-13 11:59:22 -0500270 0, 0, getPaddingTop());
Michael Jurka3cd0a592011-08-16 12:40:30 -0700271 }
272 }
273
274 @Override
Michael Jurka4a6bc552013-09-17 17:42:54 +0200275 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
276 super.onScrollChanged(l, t, oldl, oldt);
277 if (mOnScrollListener != null) {
278 mOnScrollListener.run();
279 }
280 }
281
282 public void setOnScrollListener(Runnable listener) {
283 mOnScrollListener = listener;
284 }
285
286 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700287 public int getVerticalFadingEdgeLength() {
Michael Jurka4a6bc552013-09-17 17:42:54 +0200288 if (mFadedEdgeDrawHelper != null) {
289 return mFadedEdgeDrawHelper.getVerticalFadingEdgeLength();
Michael Jurka3cd0a592011-08-16 12:40:30 -0700290 } else {
291 return super.getVerticalFadingEdgeLength();
292 }
293 }
294
295 @Override
296 public int getHorizontalFadingEdgeLength() {
Michael Jurka4a6bc552013-09-17 17:42:54 +0200297 if (mFadedEdgeDrawHelper != null) {
298 return mFadedEdgeDrawHelper.getHorizontalFadingEdgeLength();
Michael Jurka3cd0a592011-08-16 12:40:30 -0700299 } else {
300 return super.getHorizontalFadingEdgeLength();
301 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700302 }
303
304 @Override
305 protected void onFinishInflate() {
306 super.onFinishInflate();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700307 setScrollbarFadingEnabled(true);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700308 mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
John Spurlock01534782014-01-13 11:59:22 -0500309 final int leftPadding = getContext().getResources()
Jim Miller9f0f0e02011-05-17 20:06:29 -0700310 .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin);
311 setOverScrollEffectPadding(leftPadding, 0);
312 }
313
Jim Miller3c728fe2011-07-07 20:53:52 -0700314 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700315 public void onAttachedToWindow() {
Michael Jurka4a6bc552013-09-17 17:42:54 +0200316 if (mFadedEdgeDrawHelper != null) {
317 mFadedEdgeDrawHelper.onAttachedToWindowCallback(mLinearLayout, isHardwareAccelerated());
Michael Jurka3cd0a592011-08-16 12:40:30 -0700318 }
319 }
320
321 @Override
Jim Miller3c728fe2011-07-07 20:53:52 -0700322 protected void onConfigurationChanged(Configuration newConfig) {
323 super.onConfigurationChanged(newConfig);
Michael Jurka07d40462011-07-19 10:54:38 -0700324 float densityScale = getResources().getDisplayMetrics().density;
325 mSwipeHelper.setDensityScale(densityScale);
John Spurlock01534782014-01-13 11:59:22 -0500326 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
Michael Jurka07d40462011-07-19 10:54:38 -0700327 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
Jim Miller3c728fe2011-07-07 20:53:52 -0700328 }
329
Jim Miller9f0f0e02011-05-17 20:06:29 -0700330 private void setOverScrollEffectPadding(int leftPadding, int i) {
331 // TODO Add to (Vertical)ScrollView
332 }
333
334 @Override
335 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
336 super.onSizeChanged(w, h, oldw, oldh);
Chet Haase81abe872011-08-10 13:53:43 -0700337
338 // Skip this work if a transition is running; it sets the scroll values independently
339 // and should not have those animated values clobbered by this logic
340 LayoutTransition transition = mLinearLayout.getLayoutTransition();
341 if (transition != null && transition.isRunning()) {
342 return;
343 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700344 // Keep track of the last visible item in the list so we can restore it
345 // to the bottom when the orientation changes.
346 mLastScrollPosition = scrollPositionOfMostRecent();
347
348 // This has to happen post-layout, so run it "in the future"
349 post(new Runnable() {
350 public void run() {
Chet Haase81abe872011-08-10 13:53:43 -0700351 // Make sure we're still not clobbering the transition-set values, since this
352 // runnable launches asynchronously
353 LayoutTransition transition = mLinearLayout.getLayoutTransition();
354 if (transition == null || !transition.isRunning()) {
355 scrollTo(0, mLastScrollPosition);
356 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700357 }
358 });
359 }
360
Michael Jurkaab48b682011-09-12 15:39:45 -0700361 public void setAdapter(TaskDescriptionAdapter adapter) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700362 mAdapter = adapter;
363 mAdapter.registerDataSetObserver(new DataSetObserver() {
364 public void onChanged() {
365 update();
366 }
367
368 public void onInvalidated() {
369 update();
370 }
371 });
Michael Jurka99a96552012-01-27 17:23:38 -0800372
373 DisplayMetrics dm = getResources().getDisplayMetrics();
374 int childWidthMeasureSpec =
375 MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST);
376 int childheightMeasureSpec =
377 MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST);
378 View child = mAdapter.createView(mLinearLayout);
379 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
380 mNumItemsInOneScreenful =
381 (int) FloatMath.ceil(dm.heightPixels / (float) child.getMeasuredHeight());
Michael Jurka261277e2012-06-01 04:06:45 -0700382 addToRecycledViews(child);
Michael Jurka99a96552012-01-27 17:23:38 -0800383
384 for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) {
Michael Jurka261277e2012-06-01 04:06:45 -0700385 addToRecycledViews(mAdapter.createView(mLinearLayout));
Michael Jurka99a96552012-01-27 17:23:38 -0800386 }
387 }
388
389 public int numItemsInOneScreenful() {
390 return mNumItemsInOneScreenful;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700391 }
392
393 @Override
394 public void setLayoutTransition(LayoutTransition transition) {
395 // The layout transition applies to our embedded LinearLayout
396 mLinearLayout.setLayoutTransition(transition);
397 }
398
Jim Miller9f0f0e02011-05-17 20:06:29 -0700399 public void setCallback(RecentsCallback callback) {
400 mCallback = callback;
401 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700402}