TaskStackViewLayoutAlgorithm.java revision e41ab847a5c12cb7a6a5b2d0f40e101bc9ea6b59
1/* 2 * Copyright (C) 2014 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.recents.views; 18 19import android.graphics.Rect; 20import com.android.systemui.recents.RecentsConfiguration; 21import com.android.systemui.recents.misc.Utilities; 22import com.android.systemui.recents.model.Task; 23 24import java.util.ArrayList; 25import java.util.HashMap; 26 27/* The layout logic for a TaskStackView. 28 * 29 * We are using a curve that defines the curve of the tasks as that go back in the recents list. 30 * The curve is defined such that at curve progress p = 0 is the end of the curve (the top of the 31 * stack rect), and p = 1 at the start of the curve and the bottom of the stack rect. 32 */ 33public class TaskStackViewLayoutAlgorithm { 34 35 // These are all going to change 36 static final float StackPeekMinScale = 0.825f; // The min scale of the last card in the peek area 37 38 RecentsConfiguration mConfig; 39 40 // The various rects that define the stack view 41 Rect mViewRect = new Rect(); 42 Rect mStackVisibleRect = new Rect(); 43 Rect mStackRect = new Rect(); 44 Rect mTaskRect = new Rect(); 45 46 // The min/max scroll progress 47 float mMinScrollP; 48 float mMaxScrollP; 49 float mInitialScrollP; 50 int mWithinAffiliationOffset; 51 int mBetweenAffiliationOffset; 52 HashMap<Task.TaskKey, Float> mTaskProgressMap = new HashMap<Task.TaskKey, Float>(); 53 54 // Log function 55 static final float XScale = 1.75f; // The large the XScale, the longer the flat area of the curve 56 static final float LogBase = 300; 57 static final int PrecisionSteps = 250; 58 static float[] xp; 59 static float[] px; 60 61 public TaskStackViewLayoutAlgorithm(RecentsConfiguration config) { 62 mConfig = config; 63 mWithinAffiliationOffset = mConfig.taskBarHeight; 64 mBetweenAffiliationOffset = 4 * mConfig.taskBarHeight; 65 66 // Precompute the path 67 initializeCurve(); 68 } 69 70 /** Computes the stack and task rects */ 71 public void computeRects(int windowWidth, int windowHeight, Rect taskStackBounds) { 72 // Compute the stack rects 73 mViewRect.set(0, 0, windowWidth, windowHeight); 74 mStackRect.set(taskStackBounds); 75 mStackVisibleRect.set(taskStackBounds); 76 mStackVisibleRect.bottom = mViewRect.bottom; 77 78 int widthPadding = (int) (mConfig.taskStackWidthPaddingPct * mStackRect.width()); 79 int heightPadding = mConfig.taskStackTopPaddingPx; 80 mStackRect.inset(widthPadding, heightPadding); 81 82 // Compute the task rect 83 int size = mStackRect.width(); 84 int left = mStackRect.left + (mStackRect.width() - size) / 2; 85 mTaskRect.set(left, mStackRect.top, 86 left + size, mStackRect.top + size); 87 } 88 89 /** Computes the minimum and maximum scroll progress values. This method may be called before 90 * the RecentsConfiguration is set, so we need to pass in the alt-tab state. */ 91 void computeMinMaxScroll(ArrayList<Task> tasks, boolean launchedWithAltTab) { 92 // Clear the progress map 93 mTaskProgressMap.clear(); 94 95 // Return early if we have no tasks 96 if (tasks.isEmpty()) { 97 mMinScrollP = mMaxScrollP = 0; 98 return; 99 } 100 101 int taskHeight = mTaskRect.height(); 102 float pAtBottomOfStackRect = screenYToCurveProgress(mStackVisibleRect.bottom); 103 float pWithinAffiliateOffset = pAtBottomOfStackRect - 104 screenYToCurveProgress(mStackVisibleRect.bottom - mWithinAffiliationOffset); 105 float pBetweenAffiliateOffset = pAtBottomOfStackRect - 106 screenYToCurveProgress(mStackVisibleRect.bottom - mBetweenAffiliationOffset); 107 float pTaskHeightOffset = pAtBottomOfStackRect - 108 screenYToCurveProgress(mStackVisibleRect.bottom - taskHeight); 109 float pNavBarOffset = pAtBottomOfStackRect - 110 screenYToCurveProgress(mStackVisibleRect.bottom - (mStackVisibleRect.bottom - mStackRect.bottom)); 111 112 // Update the task offsets 113 float pAtBackMostCardTop = screenYToCurveProgress(mStackVisibleRect.top + 114 (mStackVisibleRect.height() - taskHeight) / 2); 115 float pAtFrontMostCardTop = pAtBackMostCardTop; 116 float pAtSecondFrontMostCardTop = pAtBackMostCardTop; 117 int taskCount = tasks.size(); 118 for (int i = 0; i < taskCount; i++) { 119 Task task = tasks.get(i); 120 mTaskProgressMap.put(task.key, pAtFrontMostCardTop); 121 122 if (i < (taskCount - 1)) { 123 // Increment the peek height 124 float pPeek = task.group.isFrontMostTask(task) ? pBetweenAffiliateOffset : 125 pWithinAffiliateOffset; 126 pAtSecondFrontMostCardTop = pAtFrontMostCardTop; 127 pAtFrontMostCardTop += pPeek; 128 } 129 } 130 131 mMinScrollP = 0f; 132 mMaxScrollP = pAtFrontMostCardTop - ((1f - pTaskHeightOffset - pNavBarOffset)); 133 if (launchedWithAltTab) { 134 // Center the second most task, since that will be focused first 135 mInitialScrollP = pAtSecondFrontMostCardTop - 0.5f; 136 } else { 137 mInitialScrollP = pAtSecondFrontMostCardTop - ((1f - pTaskHeightOffset - pNavBarOffset)); 138 } 139 } 140 141 /** Update/get the transform */ 142 public TaskViewTransform getStackTransform(Task task, float stackScroll, TaskViewTransform transformOut, 143 TaskViewTransform prevTransform) { 144 // Return early if we have an invalid index 145 if (task == null) { 146 transformOut.reset(); 147 return transformOut; 148 } 149 return getStackTransform(mTaskProgressMap.get(task.key), stackScroll, transformOut, prevTransform); 150 } 151 152 /** Update/get the transform */ 153 public TaskViewTransform getStackTransform(float taskProgress, float stackScroll, TaskViewTransform transformOut, TaskViewTransform prevTransform) { 154 float pTaskRelative = taskProgress - stackScroll; 155 float pBounded = Math.max(0, Math.min(pTaskRelative, 1f)); 156 // If the task top is outside of the bounds below the screen, then immediately reset it 157 if (pTaskRelative > 1f) { 158 transformOut.reset(); 159 transformOut.rect.set(mTaskRect); 160 return transformOut; 161 } 162 // The check for the top is trickier, since we want to show the next task if it is at all 163 // visible, even if p < 0. 164 if (pTaskRelative < 0f) { 165 if (prevTransform != null && Float.compare(prevTransform.p, 0f) <= 0) { 166 transformOut.reset(); 167 transformOut.rect.set(mTaskRect); 168 return transformOut; 169 } 170 } 171 float scale = curveProgressToScale(pBounded); 172 int scaleYOffset = (int) (((1f - scale) * mTaskRect.height()) / 2); 173 int minZ = mConfig.taskViewTranslationZMinPx; 174 int maxZ = mConfig.taskViewTranslationZMaxPx; 175 transformOut.scale = scale; 176 transformOut.translationY = curveProgressToScreenY(pBounded) - mStackVisibleRect.top - 177 scaleYOffset; 178 transformOut.translationZ = Math.max(minZ, minZ + (pBounded * (maxZ - minZ))); 179 transformOut.rect.set(mTaskRect); 180 transformOut.rect.offset(0, transformOut.translationY); 181 Utilities.scaleRectAboutCenter(transformOut.rect, transformOut.scale); 182 transformOut.visible = true; 183 transformOut.p = pTaskRelative; 184 return transformOut; 185 } 186 187 /** 188 * Returns the scroll to such task top = 1f; 189 */ 190 float getStackScrollForTaskIndex(Task t) { 191 return mTaskProgressMap.get(t.key); 192 } 193 194 /** Initializes the curve. */ 195 public static void initializeCurve() { 196 if (xp != null && px != null) return; 197 xp = new float[PrecisionSteps + 1]; 198 px = new float[PrecisionSteps + 1]; 199 200 // Approximate f(x) 201 float[] fx = new float[PrecisionSteps + 1]; 202 float step = 1f / PrecisionSteps; 203 float x = 0; 204 for (int xStep = 0; xStep <= PrecisionSteps; xStep++) { 205 fx[xStep] = logFunc(x); 206 x += step; 207 } 208 // Calculate the arc length for x:1->0 209 float pLength = 0; 210 float[] dx = new float[PrecisionSteps + 1]; 211 dx[0] = 0; 212 for (int xStep = 1; xStep < PrecisionSteps; xStep++) { 213 dx[xStep] = (float) Math.sqrt(Math.pow(fx[xStep] - fx[xStep - 1], 2) + Math.pow(step, 2)); 214 pLength += dx[xStep]; 215 } 216 // Approximate p(x), a function of cumulative progress with x, normalized to 0..1 217 float p = 0; 218 px[0] = 0f; 219 px[PrecisionSteps] = 1f; 220 for (int xStep = 1; xStep <= PrecisionSteps; xStep++) { 221 p += Math.abs(dx[xStep] / pLength); 222 px[xStep] = p; 223 } 224 // Given p(x), calculate the inverse function x(p). This assumes that x(p) is also a valid 225 // function. 226 int xStep = 0; 227 p = 0; 228 xp[0] = 0f; 229 xp[PrecisionSteps] = 1f; 230 for (int pStep = 0; pStep < PrecisionSteps; pStep++) { 231 // Walk forward in px and find the x where px <= p && p < px+1 232 while (xStep < PrecisionSteps) { 233 if (px[xStep] > p) break; 234 xStep++; 235 } 236 // Now, px[xStep-1] <= p < px[xStep] 237 if (xStep == 0) { 238 xp[pStep] = 0; 239 } else { 240 // Find x such that proportionally, x is correct 241 float fraction = (p - px[xStep - 1]) / (px[xStep] - px[xStep - 1]); 242 x = (xStep - 1 + fraction) * step; 243 xp[pStep] = x; 244 } 245 p += step; 246 } 247 } 248 249 /** Reverses and scales out x. */ 250 static float reverse(float x) { 251 return (-x * XScale) + 1; 252 } 253 /** The log function describing the curve. */ 254 static float logFunc(float x) { 255 return 1f - (float) (Math.pow(LogBase, reverse(x))) / (LogBase); 256 } 257 /** The inverse of the log function describing the curve. */ 258 float invLogFunc(float y) { 259 return (float) (Math.log((1f - reverse(y)) * (LogBase - 1) + 1) / Math.log(LogBase)); 260 } 261 262 /** Converts from the progress along the curve to a screen coordinate. */ 263 int curveProgressToScreenY(float p) { 264 if (p < 0 || p > 1) return mStackVisibleRect.top + (int) (p * mStackVisibleRect.height()); 265 float pIndex = p * PrecisionSteps; 266 int pFloorIndex = (int) Math.floor(pIndex); 267 int pCeilIndex = (int) Math.ceil(pIndex); 268 float xFraction = 0; 269 if (pFloorIndex < PrecisionSteps && (pCeilIndex != pFloorIndex)) { 270 float pFraction = (pIndex - pFloorIndex) / (pCeilIndex - pFloorIndex); 271 xFraction = (xp[pCeilIndex] - xp[pFloorIndex]) * pFraction; 272 } 273 float x = xp[pFloorIndex] + xFraction; 274 return mStackVisibleRect.top + (int) (x * mStackVisibleRect.height()); 275 } 276 277 /** Converts from the progress along the curve to a scale. */ 278 float curveProgressToScale(float p) { 279 if (p < 0) return StackPeekMinScale; 280 if (p > 1) return 1f; 281 float scaleRange = (1f - StackPeekMinScale); 282 float scale = StackPeekMinScale + (p * scaleRange); 283 return scale; 284 } 285 286 /** Converts from a screen coordinate to the progress along the curve. */ 287 float screenYToCurveProgress(int screenY) { 288 float x = (float) (screenY - mStackVisibleRect.top) / mStackVisibleRect.height(); 289 if (x < 0 || x > 1) return x; 290 float xIndex = x * PrecisionSteps; 291 int xFloorIndex = (int) Math.floor(xIndex); 292 int xCeilIndex = (int) Math.ceil(xIndex); 293 float pFraction = 0; 294 if (xFloorIndex < PrecisionSteps && (xCeilIndex != xFloorIndex)) { 295 float xFraction = (xIndex - xFloorIndex) / (xCeilIndex - xFloorIndex); 296 pFraction = (px[xCeilIndex] - px[xFloorIndex]) * xFraction; 297 } 298 return px[xFloorIndex] + pFraction; 299 } 300}