blob: bda195b548c0062da397a2ccd7daf1b000d83991 [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
Craig Mautnerc0ffce52014-07-01 12:38:52 -070019import android.app.ActivityManager;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
Winson Chungf5e22e72014-05-02 18:35:35 -070022import android.graphics.Color;
Winson Chung303e1ff2014-03-07 15:06:19 -080023import android.graphics.Rect;
Winson Chung54e297a2014-05-09 17:15:32 -070024import android.graphics.drawable.Drawable;
Craig Mautnerc0ffce52014-07-01 12:38:52 -070025import android.os.ParcelFileDescriptor;
Winson Chungffa2ec62014-07-03 15:54:42 -070026import com.android.systemui.recents.RecentsConfiguration;
Winson Chung303e1ff2014-03-07 15:06:19 -080027
Craig Mautnerc0ffce52014-07-01 12:38:52 -070028import java.io.IOException;
Winson Chungcdbbb7e2014-06-24 12:11:49 -070029import java.lang.reflect.InvocationTargetException;
30import java.lang.reflect.Method;
31
Winson Chung303e1ff2014-03-07 15:06:19 -080032/* Common code */
33public class Utilities {
Winson Chungcdbbb7e2014-06-24 12:11:49 -070034
35 // Reflection methods for altering shadows
36 private static Method sPropertyMethod;
37 static {
38 try {
39 Class<?> c = Class.forName("android.view.GLES20Canvas");
40 sPropertyMethod = c.getDeclaredMethod("setProperty", String.class, String.class);
41 if (!sPropertyMethod.isAccessible()) sPropertyMethod.setAccessible(true);
42 } catch (ClassNotFoundException e) {
43 e.printStackTrace();
44 } catch (NoSuchMethodException e) {
45 e.printStackTrace();
46 }
47 }
48
Winson Chung2f2ca082014-04-03 18:05:29 -070049 /**
50 * Calculates a consistent animation duration (ms) for all animations depending on the movement
51 * of the object being animated.
52 */
53 public static int calculateTranslationAnimationDuration(int distancePx) {
54 return calculateTranslationAnimationDuration(distancePx, 100);
55 }
56 public static int calculateTranslationAnimationDuration(int distancePx, int minDuration) {
57 RecentsConfiguration config = RecentsConfiguration.getInstance();
Winson Chung00350bb2014-04-04 10:37:18 -070058 return Math.max(minDuration, (int) (1000f /* ms/s */ *
59 (Math.abs(distancePx) / config.animationPxMovementPerSecond)));
Winson Chung2f2ca082014-04-03 18:05:29 -070060 }
61
Winson Chung303e1ff2014-03-07 15:06:19 -080062 /** Scales a rect about its centroid */
63 public static void scaleRectAboutCenter(Rect r, float scale) {
64 if (scale != 1.0f) {
65 int cx = r.centerX();
66 int cy = r.centerY();
67 r.offset(-cx, -cy);
68 r.left = (int) (r.left * scale + 0.5f);
69 r.top = (int) (r.top * scale + 0.5f);
70 r.right = (int) (r.right * scale + 0.5f);
71 r.bottom = (int) (r.bottom * scale + 0.5f);
72 r.offset(cx, cy);
73 }
74 }
Winson Chungf5e22e72014-05-02 18:35:35 -070075
76 /** Calculates the luminance-preserved greyscale of a given color. */
Winson Chung8eaeb7d2014-06-25 15:10:59 -070077 public static int colorToGreyscale(int color) {
Winson Chungf5e22e72014-05-02 18:35:35 -070078 return Math.round(0.2126f * Color.red(color) + 0.7152f * Color.green(color) +
79 0.0722f * Color.blue(color));
80 }
81
Winson Chung54e297a2014-05-09 17:15:32 -070082 /** Returns the ideal color to draw on top of a specified background color. */
Winson Chung8eaeb7d2014-06-25 15:10:59 -070083 public static int getIdealColorForBackgroundColorGreyscale(int greyscale, int lightRes,
84 int darkRes) {
Winson Chung54e297a2014-05-09 17:15:32 -070085 return (greyscale < 128) ? lightRes : darkRes;
86 }
87 /** Returns the ideal drawable to draw on top of a specified background color. */
Winson Chung8eaeb7d2014-06-25 15:10:59 -070088 public static Drawable getIdealResourceForBackgroundColorGreyscale(int greyscale,
89 Drawable lightRes,
90 Drawable darkRes) {
Winson Chung54e297a2014-05-09 17:15:32 -070091 return (greyscale < 128) ? lightRes : darkRes;
Winson Chungf5e22e72014-05-02 18:35:35 -070092 }
Winson Chungcdbbb7e2014-06-24 12:11:49 -070093
94 /** Sets some private shadow properties. */
95 public static void setShadowProperty(String property, String value)
96 throws IllegalAccessException, InvocationTargetException {
97 sPropertyMethod.invoke(null, property, value);
98 }
Winson Chung303e1ff2014-03-07 15:06:19 -080099}