Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 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 | |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 17 | package com.android.systemui.recents.misc; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 18 | |
Winson Chung | 353c0b9 | 2014-10-16 17:43:23 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 20 | import android.graphics.Color; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 22 | import android.graphics.RectF; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 24 | import android.util.ArraySet; |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 25 | import android.util.IntProperty; |
| 26 | import android.util.Property; |
Winson Chung | bf5dbf1 | 2014-09-16 00:58:25 +0200 | [diff] [blame] | 27 | import android.view.View; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 28 | import android.view.ViewParent; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 29 | |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 30 | import com.android.systemui.recents.model.Task; |
| 31 | import com.android.systemui.recents.views.TaskViewTransform; |
| 32 | |
| 33 | import java.util.Collections; |
| 34 | import java.util.List; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 35 | |
| 36 | /* Common code */ |
| 37 | public class Utilities { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 38 | |
Winson | 3e87474 | 2016-01-07 10:08:17 -0800 | [diff] [blame] | 39 | public static final Property<Drawable, Integer> DRAWABLE_ALPHA = |
| 40 | new IntProperty<Drawable>("drawableAlpha") { |
| 41 | @Override |
| 42 | public void setValue(Drawable object, int alpha) { |
| 43 | object.setAlpha(alpha); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public Integer get(Drawable object) { |
| 48 | return object.getAlpha(); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | public static final Property<Drawable, Rect> DRAWABLE_RECT = |
| 53 | new Property<Drawable, Rect>(Rect.class, "drawableBounds") { |
| 54 | @Override |
| 55 | public void set(Drawable object, Rect bounds) { |
| 56 | object.setBounds(bounds); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Rect get(Drawable object) { |
| 61 | return object.getBounds(); |
| 62 | } |
| 63 | }; |
| 64 | |
Winson | 05e46ca | 2016-02-05 15:40:29 -0800 | [diff] [blame] | 65 | public static final RectFEvaluator RECTF_EVALUATOR = new RectFEvaluator(); |
| 66 | |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @return the first parent walking up the view hierarchy that has the given class type. |
| 69 | * |
| 70 | * @param parentClass must be a class derived from {@link View} |
| 71 | */ |
| 72 | public static <T extends View> T findParent(View v, Class<T> parentClass) { |
| 73 | ViewParent parent = v.getParent(); |
| 74 | while (parent != null) { |
| 75 | if (parent.getClass().equals(parentClass)) { |
| 76 | return (T) parent; |
| 77 | } |
| 78 | parent = parent.getParent(); |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Initializes the {@param setOut} with the given object. |
| 85 | */ |
| 86 | public static <T> ArraySet<T> objectToSet(T obj, ArraySet<T> setOut) { |
| 87 | setOut.clear(); |
| 88 | if (obj != null) { |
| 89 | setOut.add(obj); |
| 90 | } |
| 91 | return setOut; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Replaces the contents of {@param setOut} with the contents of the {@param array}. |
| 96 | */ |
| 97 | public static <T> ArraySet<T> arrayToSet(T[] array, ArraySet<T> setOut) { |
| 98 | setOut.clear(); |
| 99 | if (array != null) { |
| 100 | Collections.addAll(setOut, array); |
| 101 | } |
| 102 | return setOut; |
| 103 | } |
| 104 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 105 | /** Scales a rect about its centroid */ |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 106 | public static void scaleRectAboutCenter(RectF r, float scale) { |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 107 | if (scale != 1.0f) { |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 108 | float cx = r.centerX(); |
| 109 | float cy = r.centerY(); |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 110 | r.offset(-cx, -cy); |
Winson | 3150e57 | 2015-10-23 15:07:24 -0700 | [diff] [blame] | 111 | r.left *= scale; |
| 112 | r.top *= scale; |
| 113 | r.right *= scale; |
| 114 | r.bottom *= scale; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 115 | r.offset(cx, cy); |
| 116 | } |
| 117 | } |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 118 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 119 | /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */ |
| 120 | public static float computeContrastBetweenColors(int bg, int fg) { |
| 121 | float bgR = Color.red(bg) / 255f; |
| 122 | float bgG = Color.green(bg) / 255f; |
| 123 | float bgB = Color.blue(bg) / 255f; |
| 124 | bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f); |
| 125 | bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f); |
| 126 | bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f); |
| 127 | float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB; |
| 128 | |
| 129 | float fgR = Color.red(fg) / 255f; |
| 130 | float fgG = Color.green(fg) / 255f; |
| 131 | float fgB = Color.blue(fg) / 255f; |
| 132 | fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f); |
| 133 | fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f); |
| 134 | fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f); |
| 135 | float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 136 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 137 | return Math.abs((fgL + 0.05f) / (bgL + 0.05f)); |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 138 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 139 | |
Winson Chung | a0e88b5 | 2014-08-11 19:25:42 -0700 | [diff] [blame] | 140 | /** Returns the base color overlaid with another overlay color with a specified alpha. */ |
| 141 | public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) { |
| 142 | return Color.rgb( |
| 143 | (int) (overlayAlpha * Color.red(baseColor) + |
| 144 | (1f - overlayAlpha) * Color.red(overlayColor)), |
| 145 | (int) (overlayAlpha * Color.green(baseColor) + |
| 146 | (1f - overlayAlpha) * Color.green(overlayColor)), |
| 147 | (int) (overlayAlpha * Color.blue(baseColor) + |
| 148 | (1f - overlayAlpha) * Color.blue(overlayColor))); |
| 149 | } |
| 150 | |
Winson Chung | 353c0b9 | 2014-10-16 17:43:23 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Cancels an animation ensuring that if it has listeners, onCancel and onEnd |
| 153 | * are not called. |
| 154 | */ |
| 155 | public static void cancelAnimationWithoutCallbacks(Animator animator) { |
| 156 | if (animator != null) { |
| 157 | animator.removeAllListeners(); |
| 158 | animator.cancel(); |
| 159 | } |
| 160 | } |
Winson | c5fd350 | 2016-01-18 15:18:37 -0800 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Updates {@param transforms} to be the same size as {@param tasks}. |
| 164 | */ |
| 165 | public static void matchTaskListSize(List<Task> tasks, List<TaskViewTransform> transforms) { |
| 166 | // We can reuse the task transforms where possible to reduce object allocation |
| 167 | int taskTransformCount = transforms.size(); |
| 168 | int taskCount = tasks.size(); |
| 169 | if (taskTransformCount < taskCount) { |
| 170 | // If there are less transforms than tasks, then add as many transforms as necessary |
| 171 | for (int i = taskTransformCount; i < taskCount; i++) { |
| 172 | transforms.add(new TaskViewTransform()); |
| 173 | } |
| 174 | } else if (taskTransformCount > taskCount) { |
| 175 | // If there are more transforms than tasks, then just subset the transform list |
| 176 | transforms.subList(taskCount, taskTransformCount).clear(); |
| 177 | } |
| 178 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 179 | } |