blob: a0dee07e374abf65617298309e74b997f5bd1ece [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 Chungff88d7b2014-07-17 12:30:07 -070020import android.content.Intent;
Winson Chungf5e22e72014-05-02 18:35:35 -070021import android.graphics.Color;
Winson Chungbf5dbf12014-09-16 00:58:25 +020022import android.graphics.Matrix;
Winson Chung303e1ff2014-03-07 15:06:19 -080023import android.graphics.Rect;
Winson Chungbf5dbf12014-09-16 00:58:25 +020024import android.view.View;
Winson Chungffa2ec62014-07-03 15:54:42 -070025import com.android.systemui.recents.RecentsConfiguration;
Winson Chung303e1ff2014-03-07 15:06:19 -080026
Winson Chungcdbbb7e2014-06-24 12:11:49 -070027import java.lang.reflect.InvocationTargetException;
28import java.lang.reflect.Method;
Winson Chungbf5dbf12014-09-16 00:58:25 +020029import java.util.ArrayList;
Winson Chungcdbbb7e2014-06-24 12:11:49 -070030
Winson Chung303e1ff2014-03-07 15:06:19 -080031/* Common code */
32public class Utilities {
Winson Chungcdbbb7e2014-06-24 12:11:49 -070033
34 // Reflection methods for altering shadows
35 private static Method sPropertyMethod;
36 static {
37 try {
38 Class<?> c = Class.forName("android.view.GLES20Canvas");
39 sPropertyMethod = c.getDeclaredMethod("setProperty", String.class, String.class);
40 if (!sPropertyMethod.isAccessible()) sPropertyMethod.setAccessible(true);
41 } catch (ClassNotFoundException e) {
42 e.printStackTrace();
43 } catch (NoSuchMethodException e) {
44 e.printStackTrace();
45 }
46 }
47
Winson Chung2f2ca082014-04-03 18:05:29 -070048 /**
49 * Calculates a consistent animation duration (ms) for all animations depending on the movement
50 * of the object being animated.
51 */
52 public static int calculateTranslationAnimationDuration(int distancePx) {
53 return calculateTranslationAnimationDuration(distancePx, 100);
54 }
55 public static int calculateTranslationAnimationDuration(int distancePx, int minDuration) {
56 RecentsConfiguration config = RecentsConfiguration.getInstance();
Winson Chung00350bb2014-04-04 10:37:18 -070057 return Math.max(minDuration, (int) (1000f /* ms/s */ *
58 (Math.abs(distancePx) / config.animationPxMovementPerSecond)));
Winson Chung2f2ca082014-04-03 18:05:29 -070059 }
60
Winson Chung303e1ff2014-03-07 15:06:19 -080061 /** Scales a rect about its centroid */
62 public static void scaleRectAboutCenter(Rect r, float scale) {
63 if (scale != 1.0f) {
64 int cx = r.centerX();
65 int cy = r.centerY();
66 r.offset(-cx, -cy);
67 r.left = (int) (r.left * scale + 0.5f);
68 r.top = (int) (r.top * scale + 0.5f);
69 r.right = (int) (r.right * scale + 0.5f);
70 r.bottom = (int) (r.bottom * scale + 0.5f);
71 r.offset(cx, cy);
72 }
73 }
Winson Chungf5e22e72014-05-02 18:35:35 -070074
Winson Chungbf5dbf12014-09-16 00:58:25 +020075 /** Maps a coorindate in a descendant view into the parent. */
76 public static float mapCoordInDescendentToSelf(View descendant, View root,
77 float[] coord, boolean includeRootScroll) {
78 ArrayList<View> ancestorChain = new ArrayList<View>();
79
80 float[] pt = {coord[0], coord[1]};
81
82 View v = descendant;
83 while(v != root && v != null) {
84 ancestorChain.add(v);
85 v = (View) v.getParent();
86 }
87 ancestorChain.add(root);
88
89 float scale = 1.0f;
90 int count = ancestorChain.size();
91 for (int i = 0; i < count; i++) {
92 View v0 = ancestorChain.get(i);
93 // For TextViews, scroll has a meaning which relates to the text position
94 // which is very strange... ignore the scroll.
95 if (v0 != descendant || includeRootScroll) {
96 pt[0] -= v0.getScrollX();
97 pt[1] -= v0.getScrollY();
98 }
99
100 v0.getMatrix().mapPoints(pt);
101 pt[0] += v0.getLeft();
102 pt[1] += v0.getTop();
103 scale *= v0.getScaleX();
104 }
105
106 coord[0] = pt[0];
107 coord[1] = pt[1];
108 return scale;
109 }
110
111 /** Maps a coordinate in the root to a descendent. */
112 public static float mapCoordInSelfToDescendent(View descendant, View root,
113 float[] coord, Matrix tmpInverseMatrix) {
114 ArrayList<View> ancestorChain = new ArrayList<View>();
115
116 float[] pt = {coord[0], coord[1]};
117
118 View v = descendant;
119 while(v != root) {
120 ancestorChain.add(v);
121 v = (View) v.getParent();
122 }
123 ancestorChain.add(root);
124
125 float scale = 1.0f;
126 int count = ancestorChain.size();
127 tmpInverseMatrix.set(Matrix.IDENTITY_MATRIX);
128 for (int i = count - 1; i >= 0; i--) {
129 View ancestor = ancestorChain.get(i);
130 View next = i > 0 ? ancestorChain.get(i-1) : null;
131
132 pt[0] += ancestor.getScrollX();
133 pt[1] += ancestor.getScrollY();
134
135 if (next != null) {
136 pt[0] -= next.getLeft();
137 pt[1] -= next.getTop();
138 next.getMatrix().invert(tmpInverseMatrix);
139 tmpInverseMatrix.mapPoints(pt);
140 scale *= next.getScaleX();
141 }
142 }
143
144 coord[0] = pt[0];
145 coord[1] = pt[1];
146 return scale;
147 }
148
Winson Chung93748a12014-07-13 17:43:31 -0700149 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
150 public static float computeContrastBetweenColors(int bg, int fg) {
151 float bgR = Color.red(bg) / 255f;
152 float bgG = Color.green(bg) / 255f;
153 float bgB = Color.blue(bg) / 255f;
154 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
155 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
156 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
157 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
158
159 float fgR = Color.red(fg) / 255f;
160 float fgG = Color.green(fg) / 255f;
161 float fgB = Color.blue(fg) / 255f;
162 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
163 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
164 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
165 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
Winson Chungf5e22e72014-05-02 18:35:35 -0700166
Winson Chung93748a12014-07-13 17:43:31 -0700167 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
Winson Chungf5e22e72014-05-02 18:35:35 -0700168 }
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700169
Winson Chunga0e88b52014-08-11 19:25:42 -0700170 /** Returns the base color overlaid with another overlay color with a specified alpha. */
171 public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
172 return Color.rgb(
173 (int) (overlayAlpha * Color.red(baseColor) +
174 (1f - overlayAlpha) * Color.red(overlayColor)),
175 (int) (overlayAlpha * Color.green(baseColor) +
176 (1f - overlayAlpha) * Color.green(overlayColor)),
177 (int) (overlayAlpha * Color.blue(baseColor) +
178 (1f - overlayAlpha) * Color.blue(overlayColor)));
179 }
180
Winson Chungcdbbb7e2014-06-24 12:11:49 -0700181 /** Sets some private shadow properties. */
182 public static void setShadowProperty(String property, String value)
183 throws IllegalAccessException, InvocationTargetException {
184 sPropertyMethod.invoke(null, property, value);
185 }
Winson Chungff88d7b2014-07-17 12:30:07 -0700186
187 /** Returns whether the specified intent is a document. */
188 public static boolean isDocument(Intent intent) {
189 int flags = intent.getFlags();
190 return (flags & Intent.FLAG_ACTIVITY_NEW_DOCUMENT) == Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
191 }
Winson Chung353c0b92014-10-16 17:43:23 -0700192
193 /**
194 * Cancels an animation ensuring that if it has listeners, onCancel and onEnd
195 * are not called.
196 */
197 public static void cancelAnimationWithoutCallbacks(Animator animator) {
198 if (animator != null) {
199 animator.removeAllListeners();
200 animator.cancel();
201 }
202 }
Winson Chung303e1ff2014-03-07 15:06:19 -0800203}