blob: 33303017355e73f196e4f4eee1cf6672aff20a2b [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;
Michael Jurka99a96552012-01-27 17:23:38 -080030import android.view.ViewConfiguration;
31import android.view.ViewTreeObserver;
32import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jim Miller9f0f0e02011-05-17 20:06:29 -070033import android.widget.HorizontalScrollView;
34import android.widget.LinearLayout;
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 Jurka07d40462011-07-19 10:54:38 -070043public class RecentsHorizontalScrollView extends HorizontalScrollView
Michael Jurka4eaa9832012-02-29 15:51:49 -080044 implements SwipeHelper.Callback, RecentsPanelView.RecentsScrollView {
Jim Miller3c728fe2011-07-07 20:53:52 -070045 private static final String TAG = RecentsPanelView.TAG;
46 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 Jurka3cd0a592011-08-16 12:40:30 -070055
Jim Miller9f0f0e02011-05-17 20:06:29 -070056 public RecentsHorizontalScrollView(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.Y, this, densityScale, pagingTouchSlop);
Michael Jurka3cd0a592011-08-16 12:40:30 -070061 mPerformanceHelper = RecentsScrollViewPerformanceHelper.create(context, attrs, this, false);
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) {
66 mSwipeHelper.setMinAlpha(minAlpha);
67 }
68
Jim Miller9f0f0e02011-05-17 20:06:29 -070069 private int scrollPositionOfMostRecent() {
70 return mLinearLayout.getWidth() - getWidth();
71 }
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 Iterator<View> recycledViews = mRecycledViews.iterator();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700101 for (int i = 0; i < mAdapter.getCount(); i++) {
Michael Jurka99a96552012-01-27 17:23:38 -0800102 View old = null;
Michael Jurkad1a040c2012-06-06 10:05:46 -0700103 if (recycledViews.hasNext()) {
104 old = recycledViews.next();
105 recycledViews.remove();
Michael Jurka99a96552012-01-27 17:23:38 -0800106 old.setVisibility(VISIBLE);
107 }
108
109 final View view = mAdapter.getView(i, old, mLinearLayout);
Michael Jurka07d40462011-07-19 10:54:38 -0700110
Michael Jurka3cd0a592011-08-16 12:40:30 -0700111 if (mPerformanceHelper != null) {
112 mPerformanceHelper.addViewCallback(view);
113 }
114
Michael Jurka7daf95d2011-09-30 11:07:30 -0700115 OnTouchListener noOpListener = new OnTouchListener() {
116 @Override
117 public boolean onTouch(View v, MotionEvent event) {
118 return true;
119 }
120 };
121
Michael Jurka7725a4e2011-09-02 15:55:27 -0700122 view.setOnClickListener(new OnClickListener() {
123 public void onClick(View v) {
124 mCallback.dismiss();
125 }
126 });
Michael Jurka0e8063a2011-09-09 15:31:55 -0700127 // We don't want a click sound when we dimiss recents
128 view.setSoundEffectsEnabled(false);
Michael Jurka7725a4e2011-09-02 15:55:27 -0700129
130 OnClickListener launchAppListener = new OnClickListener() {
Michael Jurka07d40462011-07-19 10:54:38 -0700131 public void onClick(View v) {
132 mCallback.handleOnClick(view);
133 }
Michael Jurka7725a4e2011-09-02 15:55:27 -0700134 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700135
Michael Jurka99a96552012-01-27 17:23:38 -0800136 RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) view.getTag();
137 final View thumbnailView = holder.thumbnailView;
Michael Jurka0e8063a2011-09-09 15:31:55 -0700138 OnLongClickListener longClickListener = new OnLongClickListener() {
139 public boolean onLongClick(View v) {
140 final View anchorView = view.findViewById(R.id.app_description);
Michael Jurka0e8063a2011-09-09 15:31:55 -0700141 mCallback.handleLongPress(view, anchorView, thumbnailView);
142 return true;
143 }
144 };
Michael Jurka7daf95d2011-09-30 11:07:30 -0700145 thumbnailView.setClickable(true);
146 thumbnailView.setOnClickListener(launchAppListener);
147 thumbnailView.setOnLongClickListener(longClickListener);
148
149 // We don't want to dismiss recents if a user clicks on the app title
150 // (we also don't want to launch the app either, though, because the
151 // app title is a small target and doesn't have great click feedback)
Michael Jurka7725a4e2011-09-02 15:55:27 -0700152 final View appTitle = view.findViewById(R.id.app_label);
Michael Jurka7daf95d2011-09-30 11:07:30 -0700153 appTitle.setContentDescription(" ");
154 appTitle.setOnTouchListener(noOpListener);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700155 mLinearLayout.addView(view);
156 }
Michael Jurka99a96552012-01-27 17:23:38 -0800157 setLayoutTransition(transitioner);
158
Michael Jurka841594b2012-10-02 14:42:53 +0200159 // Scroll to end after initial layout.
Michael Jurka99a96552012-01-27 17:23:38 -0800160
161 final OnGlobalLayoutListener updateScroll = new OnGlobalLayoutListener() {
162 public void onGlobalLayout() {
163 mLastScrollPosition = scrollPositionOfMostRecent();
164 scrollTo(mLastScrollPosition, 0);
Michael Jurka841594b2012-10-02 14:42:53 +0200165 final ViewTreeObserver observer = getViewTreeObserver();
Michael Jurka99a96552012-01-27 17:23:38 -0800166 if (observer.isAlive()) {
167 observer.removeOnGlobalLayoutListener(this);
168 }
169 }
170 };
Michael Jurka841594b2012-10-02 14:42:53 +0200171 getViewTreeObserver().addOnGlobalLayoutListener(updateScroll);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700172 }
173
174 @Override
Jim Millerc0d27312011-07-14 18:54:01 -0700175 public void removeViewInLayout(final View view) {
Michael Jurka07d40462011-07-19 10:54:38 -0700176 dismissChild(view);
Jim Millerc0d27312011-07-14 18:54:01 -0700177 }
178
Jim Miller9f0f0e02011-05-17 20:06:29 -0700179 public boolean onInterceptTouchEvent(MotionEvent ev) {
Jim Millerc0d27312011-07-14 18:54:01 -0700180 if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
Michael Jurka07d40462011-07-19 10:54:38 -0700181 return mSwipeHelper.onInterceptTouchEvent(ev) ||
182 super.onInterceptTouchEvent(ev);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700183 }
184
185 @Override
186 public boolean onTouchEvent(MotionEvent ev) {
Michael Jurka07d40462011-07-19 10:54:38 -0700187 return mSwipeHelper.onTouchEvent(ev) ||
188 super.onTouchEvent(ev);
189 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700190
Michael Jurka07d40462011-07-19 10:54:38 -0700191 public boolean canChildBeDismissed(View v) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700192 return true;
193 }
194
Michael Jurka07d40462011-07-19 10:54:38 -0700195 public void dismissChild(View v) {
196 mSwipeHelper.dismissChild(v, 0);
Jim Millerc0d27312011-07-14 18:54:01 -0700197 }
198
Michael Jurka07d40462011-07-19 10:54:38 -0700199 public void onChildDismissed(View v) {
Michael Jurka261277e2012-06-01 04:06:45 -0700200 addToRecycledViews(v);
Michael Jurka07d40462011-07-19 10:54:38 -0700201 mLinearLayout.removeView(v);
202 mCallback.handleSwipe(v);
Michael Jurka2db72fc2012-02-23 17:16:11 -0800203 // Restore the alpha/translation parameters to what they were before swiping
204 // (for when these items are recycled)
205 View contentView = getChildContentView(v);
206 contentView.setAlpha(1f);
207 contentView.setTranslationY(0);
Michael Jurka07d40462011-07-19 10:54:38 -0700208 }
209
210 public void onBeginDrag(View v) {
Michael Jurka13451a42011-08-22 15:54:21 -0700211 // We do this so the underlying ScrollView knows that it won't get
212 // the chance to intercept events anymore
213 requestDisallowInterceptTouchEvent(true);
Peter Ng622a9762011-08-29 10:56:53 -0700214 }
215
216 public void onDragCancelled(View v) {
Michael Jurka07d40462011-07-19 10:54:38 -0700217 }
218
219 public View getChildAtPosition(MotionEvent ev) {
220 final float x = ev.getX() + getScrollX();
221 final float y = ev.getY() + getScrollY();
222 for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
223 View item = mLinearLayout.getChildAt(i);
224 if (x >= item.getLeft() && x < item.getRight()
225 && y >= item.getTop() && y < item.getBottom()) {
226 return item;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700227 }
228 }
Michael Jurka07d40462011-07-19 10:54:38 -0700229 return null;
230 }
231
232 public View getChildContentView(View v) {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700233 return v.findViewById(R.id.recent_item);
234 }
235
236 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700237 public void draw(Canvas canvas) {
238 super.draw(canvas);
239
240 if (mPerformanceHelper != null) {
241 int paddingLeft = mPaddingLeft;
242 final boolean offsetRequired = isPaddingOffsetRequired();
243 if (offsetRequired) {
244 paddingLeft += getLeftPaddingOffset();
245 }
246
247 int left = mScrollX + paddingLeft;
248 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
249 int top = mScrollY + getFadeTop(offsetRequired);
250 int bottom = top + getFadeHeight(offsetRequired);
251
252 if (offsetRequired) {
253 right += getRightPaddingOffset();
254 bottom += getBottomPaddingOffset();
255 }
256 mPerformanceHelper.drawCallback(canvas,
257 left, right, top, bottom, mScrollX, mScrollY,
258 0, 0,
259 getLeftFadingEdgeStrength(), getRightFadingEdgeStrength());
260 }
261 }
262
263 @Override
264 public int getVerticalFadingEdgeLength() {
265 if (mPerformanceHelper != null) {
266 return mPerformanceHelper.getVerticalFadingEdgeLengthCallback();
267 } else {
268 return super.getVerticalFadingEdgeLength();
269 }
270 }
271
272 @Override
273 public int getHorizontalFadingEdgeLength() {
274 if (mPerformanceHelper != null) {
275 return mPerformanceHelper.getHorizontalFadingEdgeLengthCallback();
276 } else {
277 return super.getHorizontalFadingEdgeLength();
278 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700279 }
280
281 @Override
282 protected void onFinishInflate() {
283 super.onFinishInflate();
Jim Miller9f0f0e02011-05-17 20:06:29 -0700284 setScrollbarFadingEnabled(true);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700285 mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout);
Jim Miller9f0f0e02011-05-17 20:06:29 -0700286 final int leftPadding = mContext.getResources()
287 .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin);
288 setOverScrollEffectPadding(leftPadding, 0);
289 }
290
Jim Miller3c728fe2011-07-07 20:53:52 -0700291 @Override
Michael Jurka3cd0a592011-08-16 12:40:30 -0700292 public void onAttachedToWindow() {
293 if (mPerformanceHelper != null) {
294 mPerformanceHelper.onAttachedToWindowCallback(
295 mCallback, mLinearLayout, isHardwareAccelerated());
296 }
297 }
298
299 @Override
Jim Miller3c728fe2011-07-07 20:53:52 -0700300 protected void onConfigurationChanged(Configuration newConfig) {
301 super.onConfigurationChanged(newConfig);
Michael Jurka07d40462011-07-19 10:54:38 -0700302 float densityScale = getResources().getDisplayMetrics().density;
303 mSwipeHelper.setDensityScale(densityScale);
304 float pagingTouchSlop = ViewConfiguration.get(mContext).getScaledPagingTouchSlop();
305 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
Jim Miller3c728fe2011-07-07 20:53:52 -0700306 }
307
Jim Miller9f0f0e02011-05-17 20:06:29 -0700308 private void setOverScrollEffectPadding(int leftPadding, int i) {
Jim Miller3c728fe2011-07-07 20:53:52 -0700309 // TODO Add to (Vertical)ScrollView
Jim Miller9f0f0e02011-05-17 20:06:29 -0700310 }
311
312 @Override
313 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
314 super.onSizeChanged(w, h, oldw, oldh);
Chet Haase81abe872011-08-10 13:53:43 -0700315
316 // Skip this work if a transition is running; it sets the scroll values independently
317 // and should not have those animated values clobbered by this logic
318 LayoutTransition transition = mLinearLayout.getLayoutTransition();
319 if (transition != null && transition.isRunning()) {
320 return;
321 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700322 // Keep track of the last visible item in the list so we can restore it
323 // to the bottom when the orientation changes.
324 mLastScrollPosition = scrollPositionOfMostRecent();
325
326 // This has to happen post-layout, so run it "in the future"
327 post(new Runnable() {
328 public void run() {
Chet Haase81abe872011-08-10 13:53:43 -0700329 // Make sure we're still not clobbering the transition-set values, since this
330 // runnable launches asynchronously
331 LayoutTransition transition = mLinearLayout.getLayoutTransition();
332 if (transition == null || !transition.isRunning()) {
333 scrollTo(mLastScrollPosition, 0);
334 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700335 }
336 });
337 }
338
Michael Jurkaab48b682011-09-12 15:39:45 -0700339 public void setAdapter(TaskDescriptionAdapter adapter) {
Jim Miller9f0f0e02011-05-17 20:06:29 -0700340 mAdapter = adapter;
341 mAdapter.registerDataSetObserver(new DataSetObserver() {
342 public void onChanged() {
343 update();
344 }
345
346 public void onInvalidated() {
347 update();
348 }
349 });
Michael Jurka99a96552012-01-27 17:23:38 -0800350 DisplayMetrics dm = getResources().getDisplayMetrics();
351 int childWidthMeasureSpec =
352 MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST);
353 int childheightMeasureSpec =
354 MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST);
355 View child = mAdapter.createView(mLinearLayout);
356 child.measure(childWidthMeasureSpec, childheightMeasureSpec);
357 mNumItemsInOneScreenful =
358 (int) FloatMath.ceil(dm.widthPixels / (float) child.getMeasuredWidth());
Michael Jurka261277e2012-06-01 04:06:45 -0700359 addToRecycledViews(child);
Michael Jurka99a96552012-01-27 17:23:38 -0800360
361 for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) {
Michael Jurka261277e2012-06-01 04:06:45 -0700362 addToRecycledViews(mAdapter.createView(mLinearLayout));
Michael Jurka99a96552012-01-27 17:23:38 -0800363 }
364 }
365
366 public int numItemsInOneScreenful() {
367 return mNumItemsInOneScreenful;
Jim Miller9f0f0e02011-05-17 20:06:29 -0700368 }
369
370 @Override
371 public void setLayoutTransition(LayoutTransition transition) {
372 // The layout transition applies to our embedded LinearLayout
373 mLinearLayout.setLayoutTransition(transition);
374 }
375
Jim Miller9f0f0e02011-05-17 20:06:29 -0700376 public void setCallback(RecentsCallback callback) {
377 mCallback = callback;
378 }
Jim Miller9f0f0e02011-05-17 20:06:29 -0700379}