blob: 607e155d92f6ff2ed7a0f73c209bb342eb9f66ce [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 Chungf5e22e72014-05-02 18:35:35 -070019import android.graphics.Color;
Winson Chung303e1ff2014-03-07 15:06:19 -080020import android.graphics.Rect;
Winson Chungffa2ec62014-07-03 15:54:42 -070021import com.android.systemui.recents.RecentsConfiguration;
Winson Chung303e1ff2014-03-07 15:06:19 -080022
Winson Chungcdbbb7e2014-06-24 12:11:49 -070023import java.lang.reflect.InvocationTargetException;
24import java.lang.reflect.Method;
25
Winson Chung303e1ff2014-03-07 15:06:19 -080026/* Common code */
27public class Utilities {
Winson Chungcdbbb7e2014-06-24 12:11:49 -070028
29 // Reflection methods for altering shadows
30 private static Method sPropertyMethod;
31 static {
32 try {
33 Class<?> c = Class.forName("android.view.GLES20Canvas");
34 sPropertyMethod = c.getDeclaredMethod("setProperty", String.class, String.class);
35 if (!sPropertyMethod.isAccessible()) sPropertyMethod.setAccessible(true);
36 } catch (ClassNotFoundException e) {
37 e.printStackTrace();
38 } catch (NoSuchMethodException e) {
39 e.printStackTrace();
40 }
41 }
42
Winson Chung2f2ca082014-04-03 18:05:29 -070043 /**
44 * Calculates a consistent animation duration (ms) for all animations depending on the movement
45 * of the object being animated.
46 */
47 public static int calculateTranslationAnimationDuration(int distancePx) {
48 return calculateTranslationAnimationDuration(distancePx, 100);
49 }
50 public static int calculateTranslationAnimationDuration(int distancePx, int minDuration) {
51 RecentsConfiguration config = RecentsConfiguration.getInstance();
Winson Chung00350bb2014-04-04 10:37:18 -070052 return Math.max(minDuration, (int) (1000f /* ms/s */ *
53 (Math.abs(distancePx) / config.animationPxMovementPerSecond)));
Winson Chung2f2ca082014-04-03 18:05:29 -070054 }
55
Winson Chung303e1ff2014-03-07 15:06:19 -080056 /** Scales a rect about its centroid */
57 public static void scaleRectAboutCenter(Rect r, float scale) {
58 if (scale != 1.0f) {
59 int cx = r.centerX();
60 int cy = r.centerY();
61 r.offset(-cx, -cy);
62 r.left = (int) (r.left * scale + 0.5f);
63 r.top = (int) (r.top * scale + 0.5f);
64 r.right = (int) (r.right * scale + 0.5f);
65 r.bottom = (int) (r.bottom * scale + 0.5f);
66 r.offset(cx, cy);
67 }
68 }
Winson Chungf5e22e72014-05-02 18:35:35 -070069
Winson Chung93748a12014-07-13 17:43:31 -070070 /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
71 public static float computeContrastBetweenColors(int bg, int fg) {
72 float bgR = Color.red(bg) / 255f;
73 float bgG = Color.green(bg) / 255f;
74 float bgB = Color.blue(bg) / 255f;
75 bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
76 bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
77 bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
78 float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
79
80 float fgR = Color.red(fg) / 255f;
81 float fgG = Color.green(fg) / 255f;
82 float fgB = Color.blue(fg) / 255f;
83 fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
84 fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
85 fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
86 float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
Winson Chungf5e22e72014-05-02 18:35:35 -070087
Winson Chung93748a12014-07-13 17:43:31 -070088 return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
Winson Chungf5e22e72014-05-02 18:35:35 -070089 }
Winson Chungcdbbb7e2014-06-24 12:11:49 -070090
91 /** Sets some private shadow properties. */
92 public static void setShadowProperty(String property, String value)
93 throws IllegalAccessException, InvocationTargetException {
94 sPropertyMethod.invoke(null, property, value);
95 }
Winson Chung303e1ff2014-03-07 15:06:19 -080096}