blob: 297fe0d2693267f062a9b8b22a6a841b28cfa70b [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);
Michael Jurka07d40462011-07-19 10:54:38 -070059 float densityScale = getResources().getDisplayMetrics().density;
John Spurlock01534782014-01-13 11:59:22 -050060 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
Michael Jurka07d40462011-07-19 10:54:38 -070061 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
Michael Jurka3cd0a592011-08-16 12:40:30 -070062
Michael Jurka4a6bc552013-09-17 17:42:54 +020063 mFadedEdgeDrawHelper = FadedEdgeDrawHelper.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() {
John Spurlock01534782014-01-13 11:59:22 -050072 return mLinearLayout.getHeight() - getHeight() + getPaddingTop();
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 Jurka4a6bc552013-09-17 17:42:54 +0200116 if (mFadedEdgeDrawHelper != null) {
117 mFadedEdgeDrawHelper.addViewCallback(view);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700118 }
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 Jurka4a6bc552013-09-17 17:42:54 +0200247 public void drawFadedEdges(Canvas canvas, int left, int right, int top, int bottom) {
248 if (mFadedEdgeDrawHelper != null) {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700249 final boolean offsetRequired = isPaddingOffsetRequired();
Michael Jurka4a6bc552013-09-17 17:42:54 +0200250 mFadedEdgeDrawHelper.drawCallback(canvas,
John Spurlock01534782014-01-13 11:59:22 -0500251 left, right, top + getFadeTop(offsetRequired), bottom, getScrollX(), getScrollY(),
Michael Jurka3cd0a592011-08-16 12:40:30 -0700252 getTopFadingEdgeStrength(), getBottomFadingEdgeStrength(),
John Spurlock01534782014-01-13 11:59:22 -0500253 0, 0, getPaddingTop());
Michael Jurka3cd0a592011-08-16 12:40:30 -0700254 }
255 }
256
257 @Override
Michael Jurka4a6bc552013-09-17 17:42:54 +0200258 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
259 super.onScrollChanged(l, t, oldl, oldt);
260 if (mOnScrollListener != null) {
261 mOnScrollListener.run();
262 }
263 }
264
265 public void setOnScrollListener(Runnable listener) {
266 mOnScrollListener = listener;
267 }
268
269 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700270 public int getVerticalFadingEdgeLength() {
Michael Jurka4a6bc552013-09-17 17:42:54 +0200271 if (mFadedEdgeDrawHelper != null) {
272 return mFadedEdgeDrawHelper.getVerticalFadingEdgeLength();
Michael Jurka3cd0a592011-08-16 12:40:30 -0700273 } else {
274 return super.getVerticalFadingEdgeLength();
275 }
276 }
277
278 @Override
279 public int getHorizontalFadingEdgeLength() {
Michael Jurka4a6bc552013-09-17 17:42:54 +0200280 if (mFadedEdgeDrawHelper != null) {
281 return mFadedEdgeDrawHelper.getHorizontalFadingEdgeLength();
Michael Jurka3cd0a592011-08-16 12:40:30 -0700282 } else {
283 return super.getHorizontalFadingEdgeLength();
284 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700285 }
286
287 @Override
288 protected void onFinishInflate() {
289 super.onFinishInflate();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700290 setScrollbarFadingEnabled(true);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700291 mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
John Spurlock01534782014-01-13 11:59:22 -0500292 final int leftPadding = getContext().getResources()
Jim Miller9f0f0e02011-05-17 20:06:29 -0700293 .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin);
294 setOverScrollEffectPadding(leftPadding, 0);
295 }
296
Jim Miller3c728fe2011-07-07 20:53:52 -0700297 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700298 public void onAttachedToWindow() {
Michael Jurka4a6bc552013-09-17 17:42:54 +0200299 if (mFadedEdgeDrawHelper != null) {
300 mFadedEdgeDrawHelper.onAttachedToWindowCallback(mLinearLayout, isHardwareAccelerated());
Michael Jurka3cd0a592011-08-16 12:40:30 -0700301 }
302 }
303
304 @Override
Jim Miller3c728fe2011-07-07 20:53:52 -0700305 protected void onConfigurationChanged(Configuration newConfig) {
306 super.onConfigurationChanged(newConfig);
Michael Jurka07d40462011-07-19 10:54:38 -0700307 float densityScale = getResources().getDisplayMetrics().density;
308 mSwipeHelper.setDensityScale(densityScale);
John Spurlock01534782014-01-13 11:59:22 -0500309 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
Michael Jurka07d40462011-07-19 10:54:38 -0700310 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
Jim Miller3c728fe2011-07-07 20:53:52 -0700311 }
312
Jim Miller9f0f0e02011-05-17 20:06:29 -0700313 private void setOverScrollEffectPadding(int leftPadding, int i) {
314 // TODO Add to (Vertical)ScrollView
315 }
316
317 @Override
318 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
319 super.onSizeChanged(w, h, oldw, oldh);
Chet Haase81abe872011-08-10 13:53:43 -0700320
321 // Skip this work if a transition is running; it sets the scroll values independently
322 // and should not have those animated values clobbered by this logic
323 LayoutTransition transition = mLinearLayout.getLayoutTransition();
324 if (transition != null && transition.isRunning()) {
325 return;
326 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700327 // Keep track of the last visible item in the list so we can restore it
328 // to the bottom when the orientation changes.
329 mLastScrollPosition = scrollPositionOfMostRecent();
330
331 // This has to happen post-layout, so run it "in the future"
332 post(new Runnable() {
333 public void run() {
Chet Haase81abe872011-08-10 13:53:43 -0700334 // Make sure we're still not clobbering the transition-set values, since this
335 // runnable launches asynchronously
336 LayoutTransition transition = mLinearLayout.getLayoutTransition();
337 if (transition == null || !transition.isRunning()) {
338 scrollTo(0, mLastScrollPosition);
339 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700340 }
341 });
342 }
343
Michael Jurkaab48b682011-09-12 15:39:45 -0700344 public void setAdapter(TaskDescriptionAdapter adapter) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700345 mAdapter = adapter;
346 mAdapter.registerDataSetObserver(new DataSetObserver() {
347 public void onChanged() {
348 update();
349 }
350
351 public void onInvalidated() {
352 update();
353 }
354 });
Michael Jurka99a96552012-01-27 17:23:38 -0800355
356 DisplayMetrics dm = getResources().getDisplayMetrics();
357 int childWidthMeasureSpec =
358 MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST);
359 int childheightMeasureSpec =
360 MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST);
361 View child = mAdapter.createView(mLinearLayout);
362 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
363 mNumItemsInOneScreenful =
364 (int) FloatMath.ceil(dm.heightPixels / (float) child.getMeasuredHeight());
Michael Jurka261277e2012-06-01 04:06:45 -0700365 addToRecycledViews(child);
Michael Jurka99a96552012-01-27 17:23:38 -0800366
367 for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) {
Michael Jurka261277e2012-06-01 04:06:45 -0700368 addToRecycledViews(mAdapter.createView(mLinearLayout));
Michael Jurka99a96552012-01-27 17:23:38 -0800369 }
370 }
371
372 public int numItemsInOneScreenful() {
373 return mNumItemsInOneScreenful;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700374 }
375
376 @Override
377 public void setLayoutTransition(LayoutTransition transition) {
378 // The layout transition applies to our embedded LinearLayout
379 mLinearLayout.setLayoutTransition(transition);
380 }
381
Jim Miller9f0f0e02011-05-17 20:06:29 -0700382 public void setCallback(RecentsCallback callback) {
383 mCallback = callback;
384 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700385}