blob: 47b01134f10a51c95540af2cdd0be26e863b4f2a [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 Jurka3cd0a592011-08-16 12:40:30 -070052 private RecentsScrollViewPerformanceHelper mPerformanceHelper;
Michael Jurkad1a040c2012-06-06 10:05:46 -070053 private HashSet<View> mRecycledViews;
Michael Jurka99a96552012-01-27 17:23:38 -080054 private int mNumItemsInOneScreenful;
Michael Jurka07d40462011-07-19 10:54:38 -070055
Jim Miller9f0f0e02011-05-17 20:06:29 -070056 public RecentsVerticalScrollView(Context context, AttributeSet attrs) {
57 super(context, attrs, 0);
Michael Jurka07d40462011-07-19 10:54:38 -070058 float densityScale = getResources().getDisplayMetrics().density;
59 float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop();
60 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
Michael Jurka3cd0a592011-08-16 12:40:30 -070061
62 mPerformanceHelper = RecentsScrollViewPerformanceHelper.create(context, attrs, this, true);
Michael Jurkad1a040c2012-06-06 10:05:46 -070063 mRecycledViews = new HashSet<View>();
Jim Miller9f0f0e02011-05-17 20:06:29 -070064 }
65
Michael Jurka4eaa9832012-02-29 15:51:49 -080066 public void setMinSwipeAlpha(float minAlpha) {
67 mSwipeHelper.setMinAlpha(minAlpha);
68 }
69
Jim Miller9f0f0e02011-05-17 20:06:29 -070070 private int scrollPositionOfMostRecent() {
71 return mLinearLayout.getHeight() - getHeight();
72 }
73
Michael Jurka261277e2012-06-01 04:06:45 -070074 private void addToRecycledViews(View v) {
75 if (mRecycledViews.size() < mNumItemsInOneScreenful) {
76 mRecycledViews.add(v);
77 }
78 }
79
Michael Jurkae5923632012-10-03 15:29:36 +020080 public View findViewForTask(int persistentTaskId) {
Michael Jurkacb2522c2012-04-13 09:32:47 -070081 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
82 View v = mLinearLayout.getChildAt(i);
83 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) v.getTag();
Michael Jurkae5923632012-10-03 15:29:36 +020084 if (holder.taskDescription.persistentTaskId == persistentTaskId) {
Michael Jurkacb2522c2012-04-13 09:32:47 -070085 return v;
86 }
87 }
88 return null;
89 }
90
Jim Millerc0d27312011-07-14 18:54:01 -070091 private void update() {
Michael Jurka99a96552012-01-27 17:23:38 -080092 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
93 View v = mLinearLayout.getChildAt(i);
Michael Jurka261277e2012-06-01 04:06:45 -070094 addToRecycledViews(v);
Michael Jurka99a96552012-01-27 17:23:38 -080095 mAdapter.recycleView(v);
96 }
97 LayoutTransition transitioner = getLayoutTransition();
98 setLayoutTransition(null);
99
Jim Miller9f0f0e02011-05-17 20:06:29 -0700100 mLinearLayout.removeAllViews();
Michael Jurkad1a040c2012-06-06 10:05:46 -0700101
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700102 // Once we can clear the data associated with individual item views,
103 // we can get rid of the removeAllViews() and the code below will
104 // recycle them.
Michael Jurkad1a040c2012-06-06 10:05:46 -0700105 Iterator<View> recycledViews = mRecycledViews.iterator();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700106 for (int i = 0; i < mAdapter.getCount(); i++) {
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700107 View old = null;
Michael Jurkad1a040c2012-06-06 10:05:46 -0700108 if (recycledViews.hasNext()) {
109 old = recycledViews.next();
110 recycledViews.remove();
Michael Jurka99a96552012-01-27 17:23:38 -0800111 old.setVisibility(VISIBLE);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700112 }
113 final View view = mAdapter.getView(i, old, mLinearLayout);
Michael Jurka07d40462011-07-19 10:54:38 -0700114
Michael Jurka3cd0a592011-08-16 12:40:30 -0700115 if (mPerformanceHelper != null) {
116 mPerformanceHelper.addViewCallback(view);
117 }
118
Michael Jurka99a96552012-01-27 17:23:38 -0800119 OnTouchListener noOpListener = new OnTouchListener() {
120 @Override
121 public boolean onTouch(View v, MotionEvent event) {
122 return true;
123 }
124 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700125
Michael Jurka99a96552012-01-27 17:23:38 -0800126 view.setOnClickListener(new OnClickListener() {
127 public void onClick(View v) {
128 mCallback.dismiss();
129 }
130 });
131 // We don't want a click sound when we dimiss recents
132 view.setSoundEffectsEnabled(false);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700133
Michael Jurka99a96552012-01-27 17:23:38 -0800134 OnClickListener launchAppListener = new OnClickListener() {
135 public void onClick(View v) {
136 mCallback.handleOnClick(view);
137 }
138 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700139
Michael Jurka99a96552012-01-27 17:23:38 -0800140 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) view.getTag();
141 final View thumbnailView = holder.thumbnailView;
142 OnLongClickListener longClickListener = new OnLongClickListener() {
143 public boolean onLongClick(View v) {
144 final View anchorView = view.findViewById(R.id.app_description);
145 mCallback.handleLongPress(view, anchorView, thumbnailView);
146 return true;
147 }
148 };
149 thumbnailView.setClickable(true);
150 thumbnailView.setOnClickListener(launchAppListener);
151 thumbnailView.setOnLongClickListener(longClickListener);
Michael Jurka7daf95d2011-09-30 11:07:30 -0700152
Michael Jurka99a96552012-01-27 17:23:38 -0800153 // We don't want to dismiss recents if a user clicks on the app title
154 // (we also don't want to launch the app either, though, because the
155 // app title is a small target and doesn't have great click feedback)
156 final View appTitle = view.findViewById(R.id.app_label);
157 appTitle.setContentDescription(" ");
158 appTitle.setOnTouchListener(noOpListener);
159 final View calloutLine = view.findViewById(R.id.recents_callout_line);
Michael Jurkacb2522c2012-04-13 09:32:47 -0700160 if (calloutLine != null) {
161 calloutLine.setOnTouchListener(noOpListener);
162 }
Michael Jurka99a96552012-01-27 17:23:38 -0800163
164 mLinearLayout.addView(view);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700165 }
Michael Jurka99a96552012-01-27 17:23:38 -0800166 setLayoutTransition(transitioner);
167
Michael Jurka9a0890c2012-10-02 14:42:53 +0200168 // Scroll to end after initial layout.
Michael Jurka99a96552012-01-27 17:23:38 -0800169 final OnGlobalLayoutListener updateScroll = new OnGlobalLayoutListener() {
170 public void onGlobalLayout() {
171 mLastScrollPosition = scrollPositionOfMostRecent();
172 scrollTo(0, mLastScrollPosition);
Michael Jurka9a0890c2012-10-02 14:42:53 +0200173 final ViewTreeObserver observer = getViewTreeObserver();
Michael Jurka99a96552012-01-27 17:23:38 -0800174 if (observer.isAlive()) {
175 observer.removeOnGlobalLayoutListener(this);
176 }
177 }
178 };
Michael Jurka9a0890c2012-10-02 14:42:53 +0200179 getViewTreeObserver().addOnGlobalLayoutListener(updateScroll);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700180 }
181
182 @Override
Jim Millerc0d27312011-07-14 18:54:01 -0700183 public void removeViewInLayout(final View view) {
Michael Jurka07d40462011-07-19 10:54:38 -0700184 dismissChild(view);
Jim Millerc0d27312011-07-14 18:54:01 -0700185 }
186
Jim Miller9f0f0e02011-05-17 20:06:29 -0700187 public boolean onInterceptTouchEvent(MotionEvent ev) {
Jim Millerc0d27312011-07-14 18:54:01 -0700188 if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
Michael Jurka07d40462011-07-19 10:54:38 -0700189 return mSwipeHelper.onInterceptTouchEvent(ev) ||
190 super.onInterceptTouchEvent(ev);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700191 }
192
193 @Override
194 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurka07d40462011-07-19 10:54:38 -0700195 return mSwipeHelper.onTouchEvent(ev) ||
196 super.onTouchEvent(ev);
197 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700198
Michael Jurka07d40462011-07-19 10:54:38 -0700199 public boolean canChildBeDismissed(View v) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700200 return true;
201 }
202
Michael Jurka07d40462011-07-19 10:54:38 -0700203 public void dismissChild(View v) {
204 mSwipeHelper.dismissChild(v, 0);
Jim Millerc0d27312011-07-14 18:54:01 -0700205 }
206
Michael Jurka07d40462011-07-19 10:54:38 -0700207 public void onChildDismissed(View v) {
Michael Jurka261277e2012-06-01 04:06:45 -0700208 addToRecycledViews(v);
Michael Jurka07d40462011-07-19 10:54:38 -0700209 mLinearLayout.removeView(v);
210 mCallback.handleSwipe(v);
Michael Jurka2db72fc2012-02-23 17:16:11 -0800211 // Restore the alpha/translation parameters to what they were before swiping
212 // (for when these items are recycled)
213 View contentView = getChildContentView(v);
214 contentView.setAlpha(1f);
215 contentView.setTranslationX(0);
Michael Jurka07d40462011-07-19 10:54:38 -0700216 }
217
218 public void onBeginDrag(View v) {
Michael Jurka13451a42011-08-22 15:54:21 -0700219 // We do this so the underlying ScrollView knows that it won't get
220 // the chance to intercept events anymore
221 requestDisallowInterceptTouchEvent(true);
Peter Ng622a9762011-08-29 10:56:53 -0700222 }
223
224 public void onDragCancelled(View v) {
Michael Jurka07d40462011-07-19 10:54:38 -0700225 }
226
227 public View getChildAtPosition(MotionEvent ev) {
228 final float x = ev.getX() + getScrollX();
229 final float y = ev.getY() + getScrollY();
230 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
231 View item = mLinearLayout.getChildAt(i);
Dianne Hackbornfc8fa632011-08-17 16:20:47 -0700232 if (item.getVisibility() == View.VISIBLE
233 && x >= item.getLeft() && x < item.getRight()
234 && y >= item.getTop() && y < item.getBottom()) {
Michael Jurka07d40462011-07-19 10:54:38 -0700235 return item;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700236 }
237 }
Michael Jurka07d40462011-07-19 10:54:38 -0700238 return null;
239 }
240
241 public View getChildContentView(View v) {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700242 return v.findViewById(R.id.recent_item);
243 }
244
245 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700246 public void draw(Canvas canvas) {
247 super.draw(canvas);
248
249 if (mPerformanceHelper != null) {
250 int paddingLeft = mPaddingLeft;
251 final boolean offsetRequired = isPaddingOffsetRequired();
252 if (offsetRequired) {
253 paddingLeft += getLeftPaddingOffset();
254 }
255
256 int left = mScrollX + paddingLeft;
257 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
258 int top = mScrollY + getFadeTop(offsetRequired);
259 int bottom = top + getFadeHeight(offsetRequired);
260
261 if (offsetRequired) {
262 right += getRightPaddingOffset();
263 bottom += getBottomPaddingOffset();
264 }
265 mPerformanceHelper.drawCallback(canvas,
266 left, right, top, bottom, mScrollX, mScrollY,
267 getTopFadingEdgeStrength(), getBottomFadingEdgeStrength(),
268 0, 0);
269 }
270 }
271
272 @Override
273 public int getVerticalFadingEdgeLength() {
274 if (mPerformanceHelper != null) {
275 return mPerformanceHelper.getVerticalFadingEdgeLengthCallback();
276 } else {
277 return super.getVerticalFadingEdgeLength();
278 }
279 }
280
281 @Override
282 public int getHorizontalFadingEdgeLength() {
283 if (mPerformanceHelper != null) {
284 return mPerformanceHelper.getHorizontalFadingEdgeLengthCallback();
285 } else {
286 return super.getHorizontalFadingEdgeLength();
287 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700288 }
289
290 @Override
291 protected void onFinishInflate() {
292 super.onFinishInflate();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700293 setScrollbarFadingEnabled(true);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700294 mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700295 final int leftPadding = mContext.getResources()
296 .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin);
297 setOverScrollEffectPadding(leftPadding, 0);
298 }
299
Jim Miller3c728fe2011-07-07 20:53:52 -0700300 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700301 public void onAttachedToWindow() {
302 if (mPerformanceHelper != null) {
303 mPerformanceHelper.onAttachedToWindowCallback(
304 mCallback, mLinearLayout, isHardwareAccelerated());
305 }
306 }
307
308 @Override
Jim Miller3c728fe2011-07-07 20:53:52 -0700309 protected void onConfigurationChanged(Configuration newConfig) {
310 super.onConfigurationChanged(newConfig);
Michael Jurka07d40462011-07-19 10:54:38 -0700311 float densityScale = getResources().getDisplayMetrics().density;
312 mSwipeHelper.setDensityScale(densityScale);
313 float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop();
314 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
Jim Miller3c728fe2011-07-07 20:53:52 -0700315 }
316
Jim Miller9f0f0e02011-05-17 20:06:29 -0700317 private void setOverScrollEffectPadding(int leftPadding, int i) {
318 // TODO Add to (Vertical)ScrollView
319 }
320
321 @Override
322 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
323 super.onSizeChanged(w, h, oldw, oldh);
Chet Haase81abe872011-08-10 13:53:43 -0700324
325 // Skip this work if a transition is running; it sets the scroll values independently
326 // and should not have those animated values clobbered by this logic
327 LayoutTransition transition = mLinearLayout.getLayoutTransition();
328 if (transition != null && transition.isRunning()) {
329 return;
330 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700331 // Keep track of the last visible item in the list so we can restore it
332 // to the bottom when the orientation changes.
333 mLastScrollPosition = scrollPositionOfMostRecent();
334
335 // This has to happen post-layout, so run it "in the future"
336 post(new Runnable() {
337 public void run() {
Chet Haase81abe872011-08-10 13:53:43 -0700338 // Make sure we're still not clobbering the transition-set values, since this
339 // runnable launches asynchronously
340 LayoutTransition transition = mLinearLayout.getLayoutTransition();
341 if (transition == null || !transition.isRunning()) {
342 scrollTo(0, mLastScrollPosition);
343 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700344 }
345 });
346 }
347
348 @Override
349 protected void onVisibilityChanged(View changedView, int visibility) {
350 super.onVisibilityChanged(changedView, visibility);
351 // scroll to bottom after reloading
352 if (visibility == View.VISIBLE && changedView == this) {
353 post(new Runnable() {
354 public void run() {
355 update();
356 }
357 });
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}