Utilities.java revision c0d7058b14c24cd07912f5629c26b39b7b4673d5
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.misc; 18 19import android.animation.Animator; 20import android.graphics.Color; 21import android.graphics.Rect; 22import android.graphics.RectF; 23import android.graphics.drawable.Drawable; 24import android.util.ArraySet; 25import android.util.IntProperty; 26import android.util.Property; 27import android.view.View; 28import android.view.ViewParent; 29 30import com.android.systemui.recents.model.Task; 31import com.android.systemui.recents.views.TaskViewTransform; 32 33import java.util.Collections; 34import java.util.List; 35 36/* Common code */ 37public class Utilities { 38 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 65 /** 66 * @return the first parent walking up the view hierarchy that has the given class type. 67 * 68 * @param parentClass must be a class derived from {@link View} 69 */ 70 public static <T extends View> T findParent(View v, Class<T> parentClass) { 71 ViewParent parent = v.getParent(); 72 while (parent != null) { 73 if (parent.getClass().equals(parentClass)) { 74 return (T) parent; 75 } 76 parent = parent.getParent(); 77 } 78 return null; 79 } 80 81 /** 82 * Initializes the {@param setOut} with the given object. 83 */ 84 public static <T> ArraySet<T> objectToSet(T obj, ArraySet<T> setOut) { 85 setOut.clear(); 86 if (obj != null) { 87 setOut.add(obj); 88 } 89 return setOut; 90 } 91 92 /** 93 * Replaces the contents of {@param setOut} with the contents of the {@param array}. 94 */ 95 public static <T> ArraySet<T> arrayToSet(T[] array, ArraySet<T> setOut) { 96 setOut.clear(); 97 if (array != null) { 98 Collections.addAll(setOut, array); 99 } 100 return setOut; 101 } 102 103 /** Scales a rect about its centroid */ 104 public static void scaleRectAboutCenter(RectF r, float scale) { 105 if (scale != 1.0f) { 106 float cx = r.centerX(); 107 float cy = r.centerY(); 108 r.offset(-cx, -cy); 109 r.left *= scale; 110 r.top *= scale; 111 r.right *= scale; 112 r.bottom *= scale; 113 r.offset(cx, cy); 114 } 115 } 116 117 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */ 118 public static float computeContrastBetweenColors(int bg, int fg) { 119 float bgR = Color.red(bg) / 255f; 120 float bgG = Color.green(bg) / 255f; 121 float bgB = Color.blue(bg) / 255f; 122 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f); 123 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f); 124 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f); 125 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB; 126 127 float fgR = Color.red(fg) / 255f; 128 float fgG = Color.green(fg) / 255f; 129 float fgB = Color.blue(fg) / 255f; 130 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f); 131 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f); 132 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f); 133 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB; 134 135 return Math.abs((fgL + 0.05f) / (bgL + 0.05f)); 136 } 137 138 /** Returns the base color overlaid with another overlay color with a specified alpha. */ 139 public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) { 140 return Color.rgb( 141 (int) (overlayAlpha * Color.red(baseColor) + 142 (1f - overlayAlpha) * Color.red(overlayColor)), 143 (int) (overlayAlpha * Color.green(baseColor) + 144 (1f - overlayAlpha) * Color.green(overlayColor)), 145 (int) (overlayAlpha * Color.blue(baseColor) + 146 (1f - overlayAlpha) * Color.blue(overlayColor))); 147 } 148 149 /** 150 * Cancels an animation ensuring that if it has listeners, onCancel and onEnd 151 * are not called. 152 */ 153 public static void cancelAnimationWithoutCallbacks(Animator animator) { 154 if (animator != null) { 155 animator.removeAllListeners(); 156 animator.cancel(); 157 } 158 } 159 160 /** 161 * Updates {@param transforms} to be the same size as {@param tasks}. 162 */ 163 public static void matchTaskListSize(List<Task> tasks, List<TaskViewTransform> transforms) { 164 // We can reuse the task transforms where possible to reduce object allocation 165 int taskTransformCount = transforms.size(); 166 int taskCount = tasks.size(); 167 if (taskTransformCount < taskCount) { 168 // If there are less transforms than tasks, then add as many transforms as necessary 169 for (int i = taskTransformCount; i < taskCount; i++) { 170 transforms.add(new TaskViewTransform()); 171 } 172 } else if (taskTransformCount > taskCount) { 173 // If there are more transforms than tasks, then just subset the transform list 174 transforms.subList(taskCount, taskTransformCount).clear(); 175 } 176 } 177} 178