Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.recent; |
| 18 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Jim Miller | 3c728fe | 2011-07-07 20:53:52 -0700 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 22 | import android.database.DataSetObserver; |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 25 | import android.util.DisplayMetrics; |
| 26 | import android.util.FloatMath; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 27 | import android.util.Log; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 28 | import android.view.MotionEvent; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 29 | import android.view.View; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 30 | import android.view.ViewConfiguration; |
| 31 | import android.view.ViewTreeObserver; |
| 32 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 33 | import android.widget.HorizontalScrollView; |
| 34 | import android.widget.LinearLayout; |
| 35 | |
| 36 | import com.android.systemui.R; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 37 | import com.android.systemui.SwipeHelper; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 38 | import com.android.systemui.recent.RecentsPanelView.TaskDescriptionAdapter; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 39 | |
Michael Jurka | d1a040c | 2012-06-06 10:05:46 -0700 | [diff] [blame] | 40 | import java.util.HashSet; |
| 41 | import java.util.Iterator; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 42 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 43 | public class RecentsHorizontalScrollView extends HorizontalScrollView |
Michael Jurka | 4eaa983 | 2012-02-29 15:51:49 -0800 | [diff] [blame] | 44 | implements SwipeHelper.Callback, RecentsPanelView.RecentsScrollView { |
Jim Miller | 3c728fe | 2011-07-07 20:53:52 -0700 | [diff] [blame] | 45 | private static final String TAG = RecentsPanelView.TAG; |
| 46 | private static final boolean DEBUG = RecentsPanelView.DEBUG; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 47 | private LinearLayout mLinearLayout; |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 48 | private TaskDescriptionAdapter mAdapter; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 49 | private RecentsCallback mCallback; |
| 50 | protected int mLastScrollPosition; |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 51 | private SwipeHelper mSwipeHelper; |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 52 | private FadedEdgeDrawHelper mFadedEdgeDrawHelper; |
Michael Jurka | d1a040c | 2012-06-06 10:05:46 -0700 | [diff] [blame] | 53 | private HashSet<View> mRecycledViews; |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 54 | private int mNumItemsInOneScreenful; |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 55 | private Runnable mOnScrollListener; |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 56 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 57 | public RecentsHorizontalScrollView(Context context, AttributeSet attrs) { |
| 58 | super(context, attrs, 0); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 59 | float densityScale = getResources().getDisplayMetrics().density; |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 60 | float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 61 | mSwipeHelper = new SwipeHelper(SwipeHelper.Y, this, densityScale, pagingTouchSlop); |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 62 | mFadedEdgeDrawHelper = FadedEdgeDrawHelper.create(context, attrs, this, false); |
Michael Jurka | d1a040c | 2012-06-06 10:05:46 -0700 | [diff] [blame] | 63 | mRecycledViews = new HashSet<View>(); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Michael Jurka | 4eaa983 | 2012-02-29 15:51:49 -0800 | [diff] [blame] | 66 | public void setMinSwipeAlpha(float minAlpha) { |
| 67 | mSwipeHelper.setMinAlpha(minAlpha); |
| 68 | } |
| 69 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 70 | private int scrollPositionOfMostRecent() { |
| 71 | return mLinearLayout.getWidth() - getWidth(); |
| 72 | } |
| 73 | |
Michael Jurka | 261277e | 2012-06-01 04:06:45 -0700 | [diff] [blame] | 74 | private void addToRecycledViews(View v) { |
| 75 | if (mRecycledViews.size() < mNumItemsInOneScreenful) { |
| 76 | mRecycledViews.add(v); |
| 77 | } |
| 78 | } |
| 79 | |
Michael Jurka | e592363 | 2012-10-03 15:29:36 +0200 | [diff] [blame] | 80 | public View findViewForTask(int persistentTaskId) { |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 81 | for (int i = 0; i < mLinearLayout.getChildCount(); i++) { |
| 82 | View v = mLinearLayout.getChildAt(i); |
| 83 | RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) v.getTag(); |
Michael Jurka | e592363 | 2012-10-03 15:29:36 +0200 | [diff] [blame] | 84 | if (holder.taskDescription.persistentTaskId == persistentTaskId) { |
Michael Jurka | cb2522c | 2012-04-13 09:32:47 -0700 | [diff] [blame] | 85 | return v; |
| 86 | } |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
Jim Miller | c0d2731 | 2011-07-14 18:54:01 -0700 | [diff] [blame] | 91 | private void update() { |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 92 | for (int i = 0; i < mLinearLayout.getChildCount(); i++) { |
| 93 | View v = mLinearLayout.getChildAt(i); |
Michael Jurka | 261277e | 2012-06-01 04:06:45 -0700 | [diff] [blame] | 94 | addToRecycledViews(v); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 95 | mAdapter.recycleView(v); |
| 96 | } |
| 97 | LayoutTransition transitioner = getLayoutTransition(); |
| 98 | setLayoutTransition(null); |
| 99 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 100 | mLinearLayout.removeAllViews(); |
Michael Jurka | d1a040c | 2012-06-06 10:05:46 -0700 | [diff] [blame] | 101 | Iterator<View> recycledViews = mRecycledViews.iterator(); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 102 | for (int i = 0; i < mAdapter.getCount(); i++) { |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 103 | View old = null; |
Michael Jurka | d1a040c | 2012-06-06 10:05:46 -0700 | [diff] [blame] | 104 | if (recycledViews.hasNext()) { |
| 105 | old = recycledViews.next(); |
| 106 | recycledViews.remove(); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 107 | old.setVisibility(VISIBLE); |
| 108 | } |
| 109 | |
| 110 | final View view = mAdapter.getView(i, old, mLinearLayout); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 111 | |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 112 | if (mFadedEdgeDrawHelper != null) { |
| 113 | mFadedEdgeDrawHelper.addViewCallback(view); |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Michael Jurka | 7daf95d | 2011-09-30 11:07:30 -0700 | [diff] [blame] | 116 | OnTouchListener noOpListener = new OnTouchListener() { |
| 117 | @Override |
| 118 | public boolean onTouch(View v, MotionEvent event) { |
| 119 | return true; |
| 120 | } |
| 121 | }; |
| 122 | |
Michael Jurka | 7725a4e | 2011-09-02 15:55:27 -0700 | [diff] [blame] | 123 | view.setOnClickListener(new OnClickListener() { |
| 124 | public void onClick(View v) { |
| 125 | mCallback.dismiss(); |
| 126 | } |
| 127 | }); |
Michael Jurka | 0e8063a | 2011-09-09 15:31:55 -0700 | [diff] [blame] | 128 | // We don't want a click sound when we dimiss recents |
| 129 | view.setSoundEffectsEnabled(false); |
Michael Jurka | 7725a4e | 2011-09-02 15:55:27 -0700 | [diff] [blame] | 130 | |
| 131 | OnClickListener launchAppListener = new OnClickListener() { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 132 | public void onClick(View v) { |
| 133 | mCallback.handleOnClick(view); |
| 134 | } |
Michael Jurka | 7725a4e | 2011-09-02 15:55:27 -0700 | [diff] [blame] | 135 | }; |
Michael Jurka | 7daf95d | 2011-09-30 11:07:30 -0700 | [diff] [blame] | 136 | |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 137 | RecentsPanelView.ViewHolder holder = (RecentsPanelView.ViewHolder) view.getTag(); |
| 138 | final View thumbnailView = holder.thumbnailView; |
Michael Jurka | 0e8063a | 2011-09-09 15:31:55 -0700 | [diff] [blame] | 139 | OnLongClickListener longClickListener = new OnLongClickListener() { |
| 140 | public boolean onLongClick(View v) { |
| 141 | final View anchorView = view.findViewById(R.id.app_description); |
Michael Jurka | 0e8063a | 2011-09-09 15:31:55 -0700 | [diff] [blame] | 142 | mCallback.handleLongPress(view, anchorView, thumbnailView); |
| 143 | return true; |
| 144 | } |
| 145 | }; |
Michael Jurka | 7daf95d | 2011-09-30 11:07:30 -0700 | [diff] [blame] | 146 | thumbnailView.setClickable(true); |
| 147 | thumbnailView.setOnClickListener(launchAppListener); |
| 148 | thumbnailView.setOnLongClickListener(longClickListener); |
| 149 | |
| 150 | // We don't want to dismiss recents if a user clicks on the app title |
| 151 | // (we also don't want to launch the app either, though, because the |
| 152 | // app title is a small target and doesn't have great click feedback) |
Michael Jurka | 7725a4e | 2011-09-02 15:55:27 -0700 | [diff] [blame] | 153 | final View appTitle = view.findViewById(R.id.app_label); |
Michael Jurka | 7daf95d | 2011-09-30 11:07:30 -0700 | [diff] [blame] | 154 | appTitle.setContentDescription(" "); |
| 155 | appTitle.setOnTouchListener(noOpListener); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 156 | mLinearLayout.addView(view); |
| 157 | } |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 158 | setLayoutTransition(transitioner); |
| 159 | |
Michael Jurka | 841594b | 2012-10-02 14:42:53 +0200 | [diff] [blame] | 160 | // Scroll to end after initial layout. |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 161 | |
| 162 | final OnGlobalLayoutListener updateScroll = new OnGlobalLayoutListener() { |
| 163 | public void onGlobalLayout() { |
| 164 | mLastScrollPosition = scrollPositionOfMostRecent(); |
| 165 | scrollTo(mLastScrollPosition, 0); |
Michael Jurka | 841594b | 2012-10-02 14:42:53 +0200 | [diff] [blame] | 166 | final ViewTreeObserver observer = getViewTreeObserver(); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 167 | if (observer.isAlive()) { |
| 168 | observer.removeOnGlobalLayoutListener(this); |
| 169 | } |
| 170 | } |
| 171 | }; |
Michael Jurka | 841594b | 2012-10-02 14:42:53 +0200 | [diff] [blame] | 172 | getViewTreeObserver().addOnGlobalLayoutListener(updateScroll); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
Jim Miller | c0d2731 | 2011-07-14 18:54:01 -0700 | [diff] [blame] | 176 | public void removeViewInLayout(final View view) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 177 | dismissChild(view); |
Jim Miller | c0d2731 | 2011-07-14 18:54:01 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 180 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Jim Miller | c0d2731 | 2011-07-14 18:54:01 -0700 | [diff] [blame] | 181 | if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()"); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 182 | return mSwipeHelper.onInterceptTouchEvent(ev) || |
| 183 | super.onInterceptTouchEvent(ev); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public boolean onTouchEvent(MotionEvent ev) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 188 | return mSwipeHelper.onTouchEvent(ev) || |
| 189 | super.onTouchEvent(ev); |
| 190 | } |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 191 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 192 | public boolean canChildBeDismissed(View v) { |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 196 | public void dismissChild(View v) { |
| 197 | mSwipeHelper.dismissChild(v, 0); |
Jim Miller | c0d2731 | 2011-07-14 18:54:01 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 200 | public void onChildDismissed(View v) { |
Michael Jurka | 261277e | 2012-06-01 04:06:45 -0700 | [diff] [blame] | 201 | addToRecycledViews(v); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 202 | mLinearLayout.removeView(v); |
| 203 | mCallback.handleSwipe(v); |
Michael Jurka | 2db72fc | 2012-02-23 17:16:11 -0800 | [diff] [blame] | 204 | // Restore the alpha/translation parameters to what they were before swiping |
| 205 | // (for when these items are recycled) |
| 206 | View contentView = getChildContentView(v); |
| 207 | contentView.setAlpha(1f); |
| 208 | contentView.setTranslationY(0); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | public void onBeginDrag(View v) { |
Michael Jurka | 13451a4 | 2011-08-22 15:54:21 -0700 | [diff] [blame] | 212 | // We do this so the underlying ScrollView knows that it won't get |
| 213 | // the chance to intercept events anymore |
| 214 | requestDisallowInterceptTouchEvent(true); |
Peter Ng | 622a976 | 2011-08-29 10:56:53 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | public void onDragCancelled(View v) { |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | public View getChildAtPosition(MotionEvent ev) { |
| 221 | final float x = ev.getX() + getScrollX(); |
| 222 | final float y = ev.getY() + getScrollY(); |
| 223 | for (int i = 0; i < mLinearLayout.getChildCount(); i++) { |
| 224 | View item = mLinearLayout.getChildAt(i); |
| 225 | if (x >= item.getLeft() && x < item.getRight() |
| 226 | && y >= item.getTop() && y < item.getBottom()) { |
| 227 | return item; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 230 | return null; |
| 231 | } |
| 232 | |
| 233 | public View getChildContentView(View v) { |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 234 | return v.findViewById(R.id.recent_item); |
| 235 | } |
| 236 | |
| 237 | @Override |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 238 | public void drawFadedEdges(Canvas canvas, int left, int right, int top, int bottom) { |
| 239 | if (mFadedEdgeDrawHelper != null) { |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 240 | |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 241 | mFadedEdgeDrawHelper.drawCallback(canvas, |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 242 | left, right, top, bottom, getScrollX(), getScrollY(), |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 243 | 0, 0, |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 244 | getLeftFadingEdgeStrength(), getRightFadingEdgeStrength(), getPaddingTop()); |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 249 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 250 | super.onScrollChanged(l, t, oldl, oldt); |
| 251 | if (mOnScrollListener != null) { |
| 252 | mOnScrollListener.run(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void setOnScrollListener(Runnable listener) { |
| 257 | mOnScrollListener = listener; |
| 258 | } |
| 259 | |
| 260 | @Override |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 261 | public int getVerticalFadingEdgeLength() { |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 262 | if (mFadedEdgeDrawHelper != null) { |
| 263 | return mFadedEdgeDrawHelper.getVerticalFadingEdgeLength(); |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 264 | } else { |
| 265 | return super.getVerticalFadingEdgeLength(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public int getHorizontalFadingEdgeLength() { |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 271 | if (mFadedEdgeDrawHelper != null) { |
| 272 | return mFadedEdgeDrawHelper.getHorizontalFadingEdgeLength(); |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 273 | } else { |
| 274 | return super.getHorizontalFadingEdgeLength(); |
| 275 | } |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
| 279 | protected void onFinishInflate() { |
| 280 | super.onFinishInflate(); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 281 | setScrollbarFadingEnabled(true); |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 282 | mLinearLayout = (LinearLayout) findViewById(R.id.recents_linear_layout); |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 283 | final int leftPadding = getContext().getResources() |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 284 | .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin); |
| 285 | setOverScrollEffectPadding(leftPadding, 0); |
| 286 | } |
| 287 | |
Jim Miller | 3c728fe | 2011-07-07 20:53:52 -0700 | [diff] [blame] | 288 | @Override |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 289 | public void onAttachedToWindow() { |
Michael Jurka | 4a6bc55 | 2013-09-17 17:42:54 +0200 | [diff] [blame] | 290 | if (mFadedEdgeDrawHelper != null) { |
| 291 | mFadedEdgeDrawHelper.onAttachedToWindowCallback(mLinearLayout, isHardwareAccelerated()); |
Michael Jurka | 3cd0a59 | 2011-08-16 12:40:30 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | @Override |
Jim Miller | 3c728fe | 2011-07-07 20:53:52 -0700 | [diff] [blame] | 296 | protected void onConfigurationChanged(Configuration newConfig) { |
| 297 | super.onConfigurationChanged(newConfig); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 298 | float densityScale = getResources().getDisplayMetrics().density; |
| 299 | mSwipeHelper.setDensityScale(densityScale); |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 300 | float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); |
Michael Jurka | 07d4046 | 2011-07-19 10:54:38 -0700 | [diff] [blame] | 301 | mSwipeHelper.setPagingTouchSlop(pagingTouchSlop); |
Jim Miller | 3c728fe | 2011-07-07 20:53:52 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 304 | private void setOverScrollEffectPadding(int leftPadding, int i) { |
Jim Miller | 3c728fe | 2011-07-07 20:53:52 -0700 | [diff] [blame] | 305 | // TODO Add to (Vertical)ScrollView |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 310 | super.onSizeChanged(w, h, oldw, oldh); |
Chet Haase | 81abe87 | 2011-08-10 13:53:43 -0700 | [diff] [blame] | 311 | |
| 312 | // Skip this work if a transition is running; it sets the scroll values independently |
| 313 | // and should not have those animated values clobbered by this logic |
| 314 | LayoutTransition transition = mLinearLayout.getLayoutTransition(); |
| 315 | if (transition != null && transition.isRunning()) { |
| 316 | return; |
| 317 | } |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 318 | // Keep track of the last visible item in the list so we can restore it |
| 319 | // to the bottom when the orientation changes. |
| 320 | mLastScrollPosition = scrollPositionOfMostRecent(); |
| 321 | |
| 322 | // This has to happen post-layout, so run it "in the future" |
| 323 | post(new Runnable() { |
| 324 | public void run() { |
Chet Haase | 81abe87 | 2011-08-10 13:53:43 -0700 | [diff] [blame] | 325 | // Make sure we're still not clobbering the transition-set values, since this |
| 326 | // runnable launches asynchronously |
| 327 | LayoutTransition transition = mLinearLayout.getLayoutTransition(); |
| 328 | if (transition == null || !transition.isRunning()) { |
| 329 | scrollTo(mLastScrollPosition, 0); |
| 330 | } |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 331 | } |
| 332 | }); |
| 333 | } |
| 334 | |
Michael Jurka | ab48b68 | 2011-09-12 15:39:45 -0700 | [diff] [blame] | 335 | public void setAdapter(TaskDescriptionAdapter adapter) { |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 336 | mAdapter = adapter; |
| 337 | mAdapter.registerDataSetObserver(new DataSetObserver() { |
| 338 | public void onChanged() { |
| 339 | update(); |
| 340 | } |
| 341 | |
| 342 | public void onInvalidated() { |
| 343 | update(); |
| 344 | } |
| 345 | }); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 346 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
| 347 | int childWidthMeasureSpec = |
| 348 | MeasureSpec.makeMeasureSpec(dm.widthPixels, MeasureSpec.AT_MOST); |
| 349 | int childheightMeasureSpec = |
| 350 | MeasureSpec.makeMeasureSpec(dm.heightPixels, MeasureSpec.AT_MOST); |
| 351 | View child = mAdapter.createView(mLinearLayout); |
| 352 | child.measure(childWidthMeasureSpec, childheightMeasureSpec); |
| 353 | mNumItemsInOneScreenful = |
| 354 | (int) FloatMath.ceil(dm.widthPixels / (float) child.getMeasuredWidth()); |
Michael Jurka | 261277e | 2012-06-01 04:06:45 -0700 | [diff] [blame] | 355 | addToRecycledViews(child); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 356 | |
| 357 | for (int i = 0; i < mNumItemsInOneScreenful - 1; i++) { |
Michael Jurka | 261277e | 2012-06-01 04:06:45 -0700 | [diff] [blame] | 358 | addToRecycledViews(mAdapter.createView(mLinearLayout)); |
Michael Jurka | 99a9655 | 2012-01-27 17:23:38 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | public int numItemsInOneScreenful() { |
| 363 | return mNumItemsInOneScreenful; |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void setLayoutTransition(LayoutTransition transition) { |
| 368 | // The layout transition applies to our embedded LinearLayout |
| 369 | mLinearLayout.setLayoutTransition(transition); |
| 370 | } |
| 371 | |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 372 | public void setCallback(RecentsCallback callback) { |
| 373 | mCallback = callback; |
| 374 | } |
Jim Miller | 9f0f0e0 | 2011-05-17 20:06:29 -0700 | [diff] [blame] | 375 | } |