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 | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 19 | import android.graphics.Color; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 20 | import android.graphics.Rect; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame] | 21 | import com.android.systemui.recents.RecentsConfiguration; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 22 | |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 23 | import java.lang.reflect.InvocationTargetException; |
| 24 | import java.lang.reflect.Method; |
| 25 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 26 | /* Common code */ |
| 27 | public class Utilities { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 28 | |
| 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 Chung | 2f2ca08 | 2014-04-03 18:05:29 -0700 | [diff] [blame] | 43 | /** |
| 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 Chung | 00350bb | 2014-04-04 10:37:18 -0700 | [diff] [blame] | 52 | return Math.max(minDuration, (int) (1000f /* ms/s */ * |
| 53 | (Math.abs(distancePx) / config.animationPxMovementPerSecond))); |
Winson Chung | 2f2ca08 | 2014-04-03 18:05:29 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 56 | /** 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 Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 69 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame^] | 70 | /** 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 Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 87 | |
Winson Chung | 93748a1 | 2014-07-13 17:43:31 -0700 | [diff] [blame^] | 88 | return Math.abs((fgL + 0.05f) / (bgL + 0.05f)); |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 89 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 90 | |
| 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 Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 96 | } |