2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.recents.misc;
19 import android.animation.Animator;
20 import android.animation.AnimatorSet;
21 import android.animation.RectEvaluator;
22 import android.annotation.FloatRange;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.res.Configuration;
26 import android.content.res.Resources;
27 import android.graphics.Color;
28 import android.graphics.Rect;
29 import android.graphics.RectF;
30 import android.graphics.drawable.Drawable;
31 import android.os.Trace;
32 import android.util.ArraySet;
33 import android.util.IntProperty;
34 import android.util.Property;
35 import android.util.TypedValue;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.view.ViewParent;
39 import android.view.ViewStub;
41 import com.android.systemui.recents.model.Task;
42 import com.android.systemui.recents.views.TaskViewTransform;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
49 public class Utilities {
51 public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
52 new IntProperty<Drawable>("drawableAlpha") {
54 public void setValue(Drawable object, int alpha) {
55 object.setAlpha(alpha);
59 public Integer get(Drawable object) {
60 return object.getAlpha();
64 public static final Property<Drawable, Rect> DRAWABLE_RECT =
65 new Property<Drawable, Rect>(Rect.class, "drawableBounds") {
67 public void set(Drawable object, Rect bounds) {
68 object.setBounds(bounds);
72 public Rect get(Drawable object) {
73 return object.getBounds();
77 public static final RectFEvaluator RECTF_EVALUATOR = new RectFEvaluator();
78 public static final RectEvaluator RECT_EVALUATOR = new RectEvaluator(new Rect());
79 public static final Rect EMPTY_RECT = new Rect();
82 * @return the first parent walking up the view hierarchy that has the given class type.
84 * @param parentClass must be a class derived from {@link View}
86 public static <T extends View> T findParent(View v, Class<T> parentClass) {
87 ViewParent parent = v.getParent();
88 while (parent != null) {
89 if (parent.getClass().equals(parentClass)) {
92 parent = parent.getParent();
98 * Initializes the {@param setOut} with the given object.
100 public static <T> ArraySet<T> objectToSet(T obj, ArraySet<T> setOut) {
109 * Replaces the contents of {@param setOut} with the contents of the {@param array}.
111 public static <T> ArraySet<T> arrayToSet(T[] array, ArraySet<T> setOut) {
114 Collections.addAll(setOut, array);
120 * @return the clamped {@param value} between the provided {@param min} and {@param max}.
122 public static float clamp(float value, float min, float max) {
123 return Math.max(min, Math.min(max, value));
127 * @return the clamped {@param value} between the provided {@param min} and {@param max}.
129 public static int clamp(int value, int min, int max) {
130 return Math.max(min, Math.min(max, value));
134 * @return the clamped {@param value} between 0 and 1.
136 public static float clamp01(float value) {
137 return Math.max(0f, Math.min(1f, value));
141 * Scales the {@param value} to be proportionally between the {@param min} and
142 * {@param max} values.
144 * @param value must be between 0 and 1
146 public static float mapRange(@FloatRange(from=0.0,to=1.0) float value, float min, float max) {
147 return min + (value * (max - min));
151 * Scales the {@param value} proportionally from {@param min} and {@param max} to 0 and 1.
153 * @param value must be between {@param min} and {@param max}
155 public static float unmapRange(float value, float min, float max) {
156 return (value - min) / (max - min);
159 /** Scales a rect about its centroid */
160 public static void scaleRectAboutCenter(RectF r, float scale) {
162 float cx = r.centerX();
163 float cy = r.centerY();
173 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
174 public static float computeContrastBetweenColors(int bg, int fg) {
175 float bgR = Color.red(bg) / 255f;
176 float bgG = Color.green(bg) / 255f;
177 float bgB = Color.blue(bg) / 255f;
178 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
179 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
180 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
181 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
183 float fgR = Color.red(fg) / 255f;
184 float fgG = Color.green(fg) / 255f;
185 float fgB = Color.blue(fg) / 255f;
186 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
187 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
188 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
189 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
191 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
194 /** Returns the base color overlaid with another overlay color with a specified alpha. */
195 public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
197 (int) (overlayAlpha * Color.red(baseColor) +
198 (1f - overlayAlpha) * Color.red(overlayColor)),
199 (int) (overlayAlpha * Color.green(baseColor) +
200 (1f - overlayAlpha) * Color.green(overlayColor)),
201 (int) (overlayAlpha * Color.blue(baseColor) +
202 (1f - overlayAlpha) * Color.blue(overlayColor)));
206 * Cancels an animation ensuring that if it has listeners, onCancel and onEnd
209 public static void cancelAnimationWithoutCallbacks(Animator animator) {
210 if (animator != null && animator.isStarted()) {
211 removeAnimationListenersRecursive(animator);
217 * Recursively removes all the listeners of all children of this animator
219 public static void removeAnimationListenersRecursive(Animator animator) {
220 if (animator instanceof AnimatorSet) {
221 ArrayList<Animator> animators = ((AnimatorSet) animator).getChildAnimations();
222 for (int i = animators.size() - 1; i >= 0; i--) {
223 removeAnimationListenersRecursive(animators.get(i));
226 animator.removeAllListeners();
230 * Sets the given {@link View}'s frame from its current translation.
232 public static void setViewFrameFromTranslation(View v) {
233 RectF taskViewRect = new RectF(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
234 taskViewRect.offset(v.getTranslationX(), v.getTranslationY());
235 v.setTranslationX(0);
236 v.setTranslationY(0);
237 v.setLeftTopRightBottom((int) taskViewRect.left, (int) taskViewRect.top,
238 (int) taskViewRect.right, (int) taskViewRect.bottom);
242 * Returns a view stub for the given view id.
244 public static ViewStub findViewStubById(View v, int stubId) {
245 return (ViewStub) v.findViewById(stubId);
249 * Returns a view stub for the given view id.
251 public static ViewStub findViewStubById(Activity a, int stubId) {
252 return (ViewStub) a.findViewById(stubId);
256 * Updates {@param transforms} to be the same size as {@param tasks}.
258 public static void matchTaskListSize(List<Task> tasks, List<TaskViewTransform> transforms) {
259 // We can reuse the task transforms where possible to reduce object allocation
260 int taskTransformCount = transforms.size();
261 int taskCount = tasks.size();
262 if (taskTransformCount < taskCount) {
263 // If there are less transforms than tasks, then add as many transforms as necessary
264 for (int i = taskTransformCount; i < taskCount; i++) {
265 transforms.add(new TaskViewTransform());
267 } else if (taskTransformCount > taskCount) {
268 // If there are more transforms than tasks, then just subset the transform list
269 transforms.subList(taskCount, taskTransformCount).clear();
274 * Used for debugging, converts DP to PX.
276 public static float dpToPx(Resources res, float dp) {
277 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
281 * Adds a trace event for debugging.
283 public static void addTraceEvent(String event) {
284 Trace.traceBegin(Trace.TRACE_TAG_VIEW, event);
285 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
289 * Returns whether this view, or one of its descendants have accessibility focus.
291 public static boolean isDescendentAccessibilityFocused(View v) {
292 if (v.isAccessibilityFocused()) {
296 if (v instanceof ViewGroup) {
297 ViewGroup vg = (ViewGroup) v;
298 int childCount = vg.getChildCount();
299 for (int i = 0; i < childCount; i++) {
300 if (isDescendentAccessibilityFocused(vg.getChildAt(i))) {
309 * Returns the application configuration, which is independent of the activity's current
310 * configuration in multiwindow.
312 public static Configuration getAppConfiguration(Context context) {
313 return context.getApplicationContext().getResources().getConfiguration();
317 * Returns a lightweight dump of a rect.
319 public static String dumpRect(Rect r) {
323 return r.left + "," + r.top + "-" + r.right + "," + r.bottom;