blob: 52043f400bd7af3748cd446e4a97a64f78560992 [file] [log] [blame]
Winson Chung303e1ff2014-03-07 15:06:19 -08001/*
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 Chungffa2ec62014-07-03 15:54:42 -070017package com.android.systemui.recents.misc;
Winson Chung303e1ff2014-03-07 15:06:19 -080018
Winson Chung353c0b92014-10-16 17:43:23 -070019import android.animation.Animator;
Winson Chungf5e22e72014-05-02 18:35:35 -070020import android.graphics.Color;
Winson3e874742016-01-07 10:08:17 -080021import android.graphics.Rect;
Winson3150e572015-10-23 15:07:24 -070022import android.graphics.RectF;
Winson3e874742016-01-07 10:08:17 -080023import android.graphics.drawable.Drawable;
Winsonc5fd3502016-01-18 15:18:37 -080024import android.util.ArraySet;
Winson3e874742016-01-07 10:08:17 -080025import android.util.IntProperty;
26import android.util.Property;
Winson Chungbf5dbf12014-09-16 00:58:25 +020027import android.view.View;
Winsonbe7607a2015-10-01 17:24:51 -070028import android.view.ViewParent;
Winsonc0d70582016-01-29 10:24:39 -080029
Winsonc5fd3502016-01-18 15:18:37 -080030import com.android.systemui.recents.model.Task;
31import com.android.systemui.recents.views.TaskViewTransform;
32
33import java.util.Collections;
34import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080035
36/* Common code */
37public class Utilities {
Winson Chungcdbbb7e2014-06-24 12:11:49 -070038
Winson3e874742016-01-07 10:08:17 -080039 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
Winson05e46ca2016-02-05 15:40:29 -080065 public static final RectFEvaluator RECTF_EVALUATOR = new RectFEvaluator();
66
Winsonbe7607a2015-10-01 17:24:51 -070067 /**
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
Winsonc5fd3502016-01-18 15:18:37 -080083 /**
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 Chung303e1ff2014-03-07 15:06:19 -0800105 /** Scales a rect about its centroid */
Winson3150e572015-10-23 15:07:24 -0700106 public static void scaleRectAboutCenter(RectF r, float scale) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800107 if (scale != 1.0f) {
Winson3150e572015-10-23 15:07:24 -0700108 float cx = r.centerX();
109 float cy = r.centerY();
Winson Chung303e1ff2014-03-07 15:06:19 -0800110 r.offset(-cx, -cy);
Winson3150e572015-10-23 15:07:24 -0700111 r.left *= scale;
112 r.top *= scale;
113 r.right *= scale;
114 r.bottom *= scale;
Winson Chung303e1ff2014-03-07 15:06:19 -0800115 r.offset(cx, cy);
116 }
117 }
Winson Chungf5e22e72014-05-02 18:35:35 -0700118
Winson Chung93748a12014-07-13 17:43:31 -0700119 /** 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 Chungf5e22e72014-05-02 18:35:35 -0700136
Winson Chung93748a12014-07-13 17:43:31 -0700137 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
Winson Chungf5e22e72014-05-02 18:35:35 -0700138 }
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700139
Winson Chunga0e88b52014-08-11 19:25:42 -0700140 /** 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 Chung353c0b92014-10-16 17:43:23 -0700151 /**
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 }
Winsonc5fd3502016-01-18 15:18:37 -0800161
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 Chung303e1ff2014-03-07 15:06:19 -0800179}