blob: f3c4cc3d101dd8c18748381a5801099baa2fceb1 [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;
Winsonc5fd3502016-01-18 15:18:37 -080029import com.android.systemui.recents.model.Task;
30import com.android.systemui.recents.views.TaskViewTransform;
31
32import java.util.Collections;
33import java.util.List;
Winson Chung303e1ff2014-03-07 15:06:19 -080034
35/* Common code */
36public class Utilities {
Winson Chungcdbbb7e2014-06-24 12:11:49 -070037
Winson3e874742016-01-07 10:08:17 -080038 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
39 new IntProperty<Drawable>("drawableAlpha") {
40 @Override
41 public void setValue(Drawable object, int alpha) {
42 object.setAlpha(alpha);
43 }
44
45 @Override
46 public Integer get(Drawable object) {
47 return object.getAlpha();
48 }
49 };
50
51 public static final Property<Drawable, Rect> DRAWABLE_RECT =
52 new Property<Drawable, Rect>(Rect.class, "drawableBounds") {
53 @Override
54 public void set(Drawable object, Rect bounds) {
55 object.setBounds(bounds);
56 }
57
58 @Override
59 public Rect get(Drawable object) {
60 return object.getBounds();
61 }
62 };
63
Winsonbe7607a2015-10-01 17:24:51 -070064 /**
65 * @return the first parent walking up the view hierarchy that has the given class type.
66 *
67 * @param parentClass must be a class derived from {@link View}
68 */
69 public static <T extends View> T findParent(View v, Class<T> parentClass) {
70 ViewParent parent = v.getParent();
71 while (parent != null) {
72 if (parent.getClass().equals(parentClass)) {
73 return (T) parent;
74 }
75 parent = parent.getParent();
76 }
77 return null;
78 }
79
Winsonc5fd3502016-01-18 15:18:37 -080080 /**
81 * Initializes the {@param setOut} with the given object.
82 */
83 public static <T> ArraySet<T> objectToSet(T obj, ArraySet<T> setOut) {
84 setOut.clear();
85 if (obj != null) {
86 setOut.add(obj);
87 }
88 return setOut;
89 }
90
91 /**
92 * Replaces the contents of {@param setOut} with the contents of the {@param array}.
93 */
94 public static <T> ArraySet<T> arrayToSet(T[] array, ArraySet<T> setOut) {
95 setOut.clear();
96 if (array != null) {
97 Collections.addAll(setOut, array);
98 }
99 return setOut;
100 }
101
Winson Chung303e1ff2014-03-07 15:06:19 -0800102 /** Scales a rect about its centroid */
Winson3150e572015-10-23 15:07:24 -0700103 public static void scaleRectAboutCenter(RectF r, float scale) {
Winson Chung303e1ff2014-03-07 15:06:19 -0800104 if (scale != 1.0f) {
Winson3150e572015-10-23 15:07:24 -0700105 float cx = r.centerX();
106 float cy = r.centerY();
Winson Chung303e1ff2014-03-07 15:06:19 -0800107 r.offset(-cx, -cy);
Winson3150e572015-10-23 15:07:24 -0700108 r.left *= scale;
109 r.top *= scale;
110 r.right *= scale;
111 r.bottom *= scale;
Winson Chung303e1ff2014-03-07 15:06:19 -0800112 r.offset(cx, cy);
113 }
114 }
Winson Chungf5e22e72014-05-02 18:35:35 -0700115
Winson Chung93748a12014-07-13 17:43:31 -0700116 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
117 public static float computeContrastBetweenColors(int bg, int fg) {
118 float bgR = Color.red(bg) / 255f;
119 float bgG = Color.green(bg) / 255f;
120 float bgB = Color.blue(bg) / 255f;
121 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
122 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
123 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
124 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
125
126 float fgR = Color.red(fg) / 255f;
127 float fgG = Color.green(fg) / 255f;
128 float fgB = Color.blue(fg) / 255f;
129 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
130 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
131 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
132 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
Winson Chungf5e22e72014-05-02 18:35:35 -0700133
Winson Chung93748a12014-07-13 17:43:31 -0700134 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
Winson Chungf5e22e72014-05-02 18:35:35 -0700135 }
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700136
Winson Chunga0e88b52014-08-11 19:25:42 -0700137 /** Returns the base color overlaid with another overlay color with a specified alpha. */
138 public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
139 return Color.rgb(
140 (int) (overlayAlpha * Color.red(baseColor) +
141 (1f - overlayAlpha) * Color.red(overlayColor)),
142 (int) (overlayAlpha * Color.green(baseColor) +
143 (1f - overlayAlpha) * Color.green(overlayColor)),
144 (int) (overlayAlpha * Color.blue(baseColor) +
145 (1f - overlayAlpha) * Color.blue(overlayColor)));
146 }
147
Winson Chung353c0b92014-10-16 17:43:23 -0700148 /**
149 * Cancels an animation ensuring that if it has listeners, onCancel and onEnd
150 * are not called.
151 */
152 public static void cancelAnimationWithoutCallbacks(Animator animator) {
153 if (animator != null) {
154 animator.removeAllListeners();
155 animator.cancel();
156 }
157 }
Winsonc5fd3502016-01-18 15:18:37 -0800158
159 /**
160 * Updates {@param transforms} to be the same size as {@param tasks}.
161 */
162 public static void matchTaskListSize(List<Task> tasks, List<TaskViewTransform> transforms) {
163 // We can reuse the task transforms where possible to reduce object allocation
164 int taskTransformCount = transforms.size();
165 int taskCount = tasks.size();
166 if (taskTransformCount < taskCount) {
167 // If there are less transforms than tasks, then add as many transforms as necessary
168 for (int i = taskTransformCount; i < taskCount; i++) {
169 transforms.add(new TaskViewTransform());
170 }
171 } else if (taskTransformCount > taskCount) {
172 // If there are more transforms than tasks, then just subset the transform list
173 transforms.subList(taskCount, taskTransformCount).clear();
174 }
175 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800176}