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 Chung | bf5dbf1 | 2014-09-16 00:58:25 +0200 | [diff] [blame] | 21 | import android.graphics.Matrix; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 22 | import android.graphics.Rect; |
Winson Chung | bf5dbf1 | 2014-09-16 00:58:25 +0200 | [diff] [blame] | 23 | import android.view.View; |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame^] | 24 | import android.view.ViewParent; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 25 | |
Winson Chung | bf5dbf1 | 2014-09-16 00:58:25 +0200 | [diff] [blame] | 26 | import java.util.ArrayList; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 27 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 28 | /* Common code */ |
| 29 | public class Utilities { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 30 | |
Winson | be7607a | 2015-10-01 17:24:51 -0700 | [diff] [blame^] | 31 | /** |
| 32 | * @return the first parent walking up the view hierarchy that has the given class type. |
| 33 | * |
| 34 | * @param parentClass must be a class derived from {@link View} |
| 35 | */ |
| 36 | public static <T extends View> T findParent(View v, Class<T> parentClass) { |
| 37 | ViewParent parent = v.getParent(); |
| 38 | while (parent != null) { |
| 39 | if (parent.getClass().equals(parentClass)) { |
| 40 | return (T) parent; |
| 41 | } |
| 42 | parent = parent.getParent(); |
| 43 | } |
| 44 | return null; |
| 45 | } |
| 46 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 47 | /** Scales a rect about its centroid */ |
| 48 | public static void scaleRectAboutCenter(Rect r, float scale) { |
| 49 | if (scale != 1.0f) { |
| 50 | int cx = r.centerX(); |
| 51 | int cy = r.centerY(); |
| 52 | r.offset(-cx, -cy); |
| 53 | r.left = (int) (r.left * scale + 0.5f); |
| 54 | r.top = (int) (r.top * scale + 0.5f); |
| 55 | r.right = (int) (r.right * scale + 0.5f); |
| 56 | r.bottom = (int) (r.bottom * scale + 0.5f); |
| 57 | r.offset(cx, cy); |
| 58 | } |
| 59 | } |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 60 | |
Winson Chung | bf5dbf1 | 2014-09-16 00:58:25 +0200 | [diff] [blame] | 61 | /** Maps a coorindate in a descendant view into the parent. */ |
| 62 | public static float mapCoordInDescendentToSelf(View descendant, View root, |
| 63 | float[] coord, boolean includeRootScroll) { |
| 64 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 65 | |
| 66 | float[] pt = {coord[0], coord[1]}; |
| 67 | |
| 68 | View v = descendant; |
| 69 | while(v != root && v != null) { |
| 70 | ancestorChain.add(v); |
| 71 | v = (View) v.getParent(); |
| 72 | } |
| 73 | ancestorChain.add(root); |
| 74 | |
| 75 | float scale = 1.0f; |
| 76 | int count = ancestorChain.size(); |
| 77 | for (int i = 0; i < count; i++) { |
| 78 | View v0 = ancestorChain.get(i); |
| 79 | // For TextViews, scroll has a meaning which relates to the text position |
| 80 | // which is very strange... ignore the scroll. |
| 81 | if (v0 != descendant || includeRootScroll) { |
| 82 | pt[0] -= v0.getScrollX(); |
| 83 | pt[1] -= v0.getScrollY(); |
| 84 | } |
| 85 | |
| 86 | v0.getMatrix().mapPoints(pt); |
| 87 | pt[0] += v0.getLeft(); |
| 88 | pt[1] += v0.getTop(); |
| 89 | scale *= v0.getScaleX(); |
| 90 | } |
| 91 | |
| 92 | coord[0] = pt[0]; |
| 93 | coord[1] = pt[1]; |
| 94 | return scale; |
| 95 | } |
| 96 | |
| 97 | /** Maps a coordinate in the root to a descendent. */ |
| 98 | public static float mapCoordInSelfToDescendent(View descendant, View root, |
| 99 | float[] coord, Matrix tmpInverseMatrix) { |
| 100 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 101 | |
| 102 | float[] pt = {coord[0], coord[1]}; |
| 103 | |
| 104 | View v = descendant; |
| 105 | while(v != root) { |
| 106 | ancestorChain.add(v); |
| 107 | v = (View) v.getParent(); |
| 108 | } |
| 109 | ancestorChain.add(root); |
| 110 | |
| 111 | float scale = 1.0f; |
| 112 | int count = ancestorChain.size(); |
| 113 | tmpInverseMatrix.set(Matrix.IDENTITY_MATRIX); |
| 114 | for (int i = count - 1; i >= 0; i--) { |
| 115 | View ancestor = ancestorChain.get(i); |
| 116 | View next = i > 0 ? ancestorChain.get(i-1) : null; |
| 117 | |
| 118 | pt[0] += ancestor.getScrollX(); |
| 119 | pt[1] += ancestor.getScrollY(); |
| 120 | |
| 121 | if (next != null) { |
| 122 | pt[0] -= next.getLeft(); |
| 123 | pt[1] -= next.getTop(); |
| 124 | next.getMatrix().invert(tmpInverseMatrix); |
| 125 | tmpInverseMatrix.mapPoints(pt); |
| 126 | scale *= next.getScaleX(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | coord[0] = pt[0]; |
| 131 | coord[1] = pt[1]; |
| 132 | return scale; |
| 133 | } |
| 134 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 135 | /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */ |
| 136 | public static float computeContrastBetweenColors(int bg, int fg) { |
| 137 | float bgR = Color.red(bg) / 255f; |
| 138 | float bgG = Color.green(bg) / 255f; |
| 139 | float bgB = Color.blue(bg) / 255f; |
| 140 | bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f); |
| 141 | bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f); |
| 142 | bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f); |
| 143 | float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB; |
| 144 | |
| 145 | float fgR = Color.red(fg) / 255f; |
| 146 | float fgG = Color.green(fg) / 255f; |
| 147 | float fgB = Color.blue(fg) / 255f; |
| 148 | fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f); |
| 149 | fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f); |
| 150 | fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f); |
| 151 | float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 152 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame] | 153 | return Math.abs((fgL + 0.05f) / (bgL + 0.05f)); |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 154 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 155 | |
Winson Chung | a0e88b5 | 2014-08-11 19:25:42 -0700 | [diff] [blame] | 156 | /** Returns the base color overlaid with another overlay color with a specified alpha. */ |
| 157 | public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) { |
| 158 | return Color.rgb( |
| 159 | (int) (overlayAlpha * Color.red(baseColor) + |
| 160 | (1f - overlayAlpha) * Color.red(overlayColor)), |
| 161 | (int) (overlayAlpha * Color.green(baseColor) + |
| 162 | (1f - overlayAlpha) * Color.green(overlayColor)), |
| 163 | (int) (overlayAlpha * Color.blue(baseColor) + |
| 164 | (1f - overlayAlpha) * Color.blue(overlayColor))); |
| 165 | } |
| 166 | |
Winson Chung | 353c0b9 | 2014-10-16 17:43:23 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Cancels an animation ensuring that if it has listeners, onCancel and onEnd |
| 169 | * are not called. |
| 170 | */ |
| 171 | public static void cancelAnimationWithoutCallbacks(Animator animator) { |
| 172 | if (animator != null) { |
| 173 | animator.removeAllListeners(); |
| 174 | animator.cancel(); |
| 175 | } |
| 176 | } |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 177 | } |