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 | |
Craig Mautner | c0ffce5 | 2014-07-01 12:38:52 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.BitmapFactory; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 22 | import android.graphics.Color; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
Winson Chung | 54e297a | 2014-05-09 17:15:32 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Craig Mautner | c0ffce5 | 2014-07-01 12:38:52 -0700 | [diff] [blame] | 25 | import android.os.ParcelFileDescriptor; |
Winson Chung | ffa2ec6 | 2014-07-03 15:54:42 -0700 | [diff] [blame^] | 26 | import com.android.systemui.recents.RecentsConfiguration; |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 27 | |
Craig Mautner | c0ffce5 | 2014-07-01 12:38:52 -0700 | [diff] [blame] | 28 | import java.io.IOException; |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 29 | import java.lang.reflect.InvocationTargetException; |
| 30 | import java.lang.reflect.Method; |
| 31 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 32 | /* Common code */ |
| 33 | public class Utilities { |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 34 | |
| 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 Chung | 2f2ca08 | 2014-04-03 18:05:29 -0700 | [diff] [blame] | 49 | /** |
| 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 Chung | 00350bb | 2014-04-04 10:37:18 -0700 | [diff] [blame] | 58 | return Math.max(minDuration, (int) (1000f /* ms/s */ * |
| 59 | (Math.abs(distancePx) / config.animationPxMovementPerSecond))); |
Winson Chung | 2f2ca08 | 2014-04-03 18:05:29 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Winson Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 62 | /** 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 Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 75 | |
| 76 | /** Calculates the luminance-preserved greyscale of a given color. */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 77 | public static int colorToGreyscale(int color) { |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 78 | return Math.round(0.2126f * Color.red(color) + 0.7152f * Color.green(color) + |
| 79 | 0.0722f * Color.blue(color)); |
| 80 | } |
| 81 | |
Winson Chung | 54e297a | 2014-05-09 17:15:32 -0700 | [diff] [blame] | 82 | /** Returns the ideal color to draw on top of a specified background color. */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 83 | public static int getIdealColorForBackgroundColorGreyscale(int greyscale, int lightRes, |
| 84 | int darkRes) { |
Winson Chung | 54e297a | 2014-05-09 17:15:32 -0700 | [diff] [blame] | 85 | return (greyscale < 128) ? lightRes : darkRes; |
| 86 | } |
| 87 | /** Returns the ideal drawable to draw on top of a specified background color. */ |
Winson Chung | 8eaeb7d | 2014-06-25 15:10:59 -0700 | [diff] [blame] | 88 | public static Drawable getIdealResourceForBackgroundColorGreyscale(int greyscale, |
| 89 | Drawable lightRes, |
| 90 | Drawable darkRes) { |
Winson Chung | 54e297a | 2014-05-09 17:15:32 -0700 | [diff] [blame] | 91 | return (greyscale < 128) ? lightRes : darkRes; |
Winson Chung | f5e22e7 | 2014-05-02 18:35:35 -0700 | [diff] [blame] | 92 | } |
Winson Chung | cdbbb7e | 2014-06-24 12:11:49 -0700 | [diff] [blame] | 93 | |
| 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 Chung | 303e1ff | 2014-03-07 15:06:19 -0800 | [diff] [blame] | 99 | } |