OSDN Git Service

docs: Updating N release notes for DP3 am: 58d5504241
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / recents / misc / Utilities.java
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
17 package com.android.systemui.recents.misc;
18
19 import android.animation.Animator;
20 import android.animation.AnimatorSet;
21 import android.animation.RectEvaluator;
22 import android.annotation.FloatRange;
23 import android.app.Activity;
24 import android.content.Context;
25 import android.content.res.Configuration;
26 import android.content.res.Resources;
27 import android.graphics.Color;
28 import android.graphics.Rect;
29 import android.graphics.RectF;
30 import android.graphics.drawable.Drawable;
31 import android.os.Trace;
32 import android.util.ArraySet;
33 import android.util.IntProperty;
34 import android.util.Property;
35 import android.util.TypedValue;
36 import android.view.View;
37 import android.view.ViewParent;
38 import android.view.ViewStub;
39
40 import com.android.systemui.recents.model.Task;
41 import com.android.systemui.recents.views.TaskViewTransform;
42
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.List;
46
47 /* Common code */
48 public class Utilities {
49
50     public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
51             new IntProperty<Drawable>("drawableAlpha") {
52                 @Override
53                 public void setValue(Drawable object, int alpha) {
54                     object.setAlpha(alpha);
55                 }
56
57                 @Override
58                 public Integer get(Drawable object) {
59                     return object.getAlpha();
60                 }
61             };
62
63     public static final Property<Drawable, Rect> DRAWABLE_RECT =
64             new Property<Drawable, Rect>(Rect.class, "drawableBounds") {
65                 @Override
66                 public void set(Drawable object, Rect bounds) {
67                     object.setBounds(bounds);
68                 }
69
70                 @Override
71                 public Rect get(Drawable object) {
72                     return object.getBounds();
73                 }
74             };
75
76     public static final RectFEvaluator RECTF_EVALUATOR = new RectFEvaluator();
77     public static final RectEvaluator RECT_EVALUATOR = new RectEvaluator(new Rect());
78     public static final Rect EMPTY_RECT = new Rect();
79
80     /**
81      * @return the first parent walking up the view hierarchy that has the given class type.
82      *
83      * @param parentClass must be a class derived from {@link View}
84      */
85     public static <T extends View> T findParent(View v, Class<T> parentClass) {
86         ViewParent parent = v.getParent();
87         while (parent != null) {
88             if (parent.getClass().equals(parentClass)) {
89                 return (T) parent;
90             }
91             parent = parent.getParent();
92         }
93         return null;
94     }
95
96     /**
97      * Initializes the {@param setOut} with the given object.
98      */
99     public static <T> ArraySet<T> objectToSet(T obj, ArraySet<T> setOut) {
100         setOut.clear();
101         if (obj != null) {
102             setOut.add(obj);
103         }
104         return setOut;
105     }
106
107     /**
108      * Replaces the contents of {@param setOut} with the contents of the {@param array}.
109      */
110     public static <T> ArraySet<T> arrayToSet(T[] array, ArraySet<T> setOut) {
111         setOut.clear();
112         if (array != null) {
113             Collections.addAll(setOut, array);
114         }
115         return setOut;
116     }
117
118     /**
119      * @return the clamped {@param value} between the provided {@param min} and {@param max}.
120      */
121     public static float clamp(float value, float min, float max) {
122         return Math.max(min, Math.min(max, value));
123     }
124
125     /**
126      * @return the clamped {@param value} between the provided {@param min} and {@param max}.
127      */
128     public static int clamp(int value, int min, int max) {
129         return Math.max(min, Math.min(max, value));
130     }
131
132     /**
133      * @return the clamped {@param value} between 0 and 1.
134      */
135     public static float clamp01(float value) {
136         return Math.max(0f, Math.min(1f, value));
137     }
138
139     /**
140      * Scales the {@param value} to be proportionally between the {@param min} and
141      * {@param max} values.
142      *
143      * @param value must be between 0 and 1
144      */
145     public static float mapRange(@FloatRange(from=0.0,to=1.0) float value, float min, float max) {
146         return min + (value * (max - min));
147     }
148
149     /**
150      * Scales the {@param value} proportionally from {@param min} and {@param max} to 0 and 1.
151      *
152      * @param value must be between {@param min} and {@param max}
153      */
154     public static float unmapRange(float value, float min, float max) {
155         return (value - min) / (max - min);
156     }
157
158     /** Scales a rect about its centroid */
159     public static void scaleRectAboutCenter(RectF r, float scale) {
160         if (scale != 1.0f) {
161             float cx = r.centerX();
162             float cy = r.centerY();
163             r.offset(-cx, -cy);
164             r.left *= scale;
165             r.top *= scale;
166             r.right *= scale;
167             r.bottom *= scale;
168             r.offset(cx, cy);
169         }
170     }
171
172     /** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
173     public static float computeContrastBetweenColors(int bg, int fg) {
174         float bgR = Color.red(bg) / 255f;
175         float bgG = Color.green(bg) / 255f;
176         float bgB = Color.blue(bg) / 255f;
177         bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
178         bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
179         bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
180         float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
181         
182         float fgR = Color.red(fg) / 255f;
183         float fgG = Color.green(fg) / 255f;
184         float fgB = Color.blue(fg) / 255f;
185         fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
186         fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
187         fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
188         float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
189
190         return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
191     }
192
193     /** Returns the base color overlaid with another overlay color with a specified alpha. */
194     public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
195         return Color.rgb(
196             (int) (overlayAlpha * Color.red(baseColor) +
197                     (1f - overlayAlpha) * Color.red(overlayColor)),
198             (int) (overlayAlpha * Color.green(baseColor) +
199                     (1f - overlayAlpha) * Color.green(overlayColor)),
200             (int) (overlayAlpha * Color.blue(baseColor) +
201                     (1f - overlayAlpha) * Color.blue(overlayColor)));
202     }
203
204     /**
205      * Cancels an animation ensuring that if it has listeners, onCancel and onEnd
206      * are not called.
207      */
208     public static void cancelAnimationWithoutCallbacks(Animator animator) {
209         if (animator != null && animator.isStarted()) {
210             removeAnimationListenersRecursive(animator);
211             animator.cancel();
212         }
213     }
214
215     /**
216      * Recursively removes all the listeners of all children of this animator
217      */
218     public static void removeAnimationListenersRecursive(Animator animator) {
219         if (animator instanceof AnimatorSet) {
220             ArrayList<Animator> animators = ((AnimatorSet) animator).getChildAnimations();
221             for (int i = animators.size() - 1; i >= 0; i--) {
222                 removeAnimationListenersRecursive(animators.get(i));
223             }
224         }
225         animator.removeAllListeners();
226     }
227
228     /**
229      * Sets the given {@link View}'s frame from its current translation.
230      */
231     public static void setViewFrameFromTranslation(View v) {
232         RectF taskViewRect = new RectF(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
233         taskViewRect.offset(v.getTranslationX(), v.getTranslationY());
234         v.setTranslationX(0);
235         v.setTranslationY(0);
236         v.setLeftTopRightBottom((int) taskViewRect.left, (int) taskViewRect.top,
237                 (int) taskViewRect.right, (int) taskViewRect.bottom);
238     }
239
240     /**
241      * Returns a view stub for the given view id.
242      */
243     public static ViewStub findViewStubById(View v, int stubId) {
244         return (ViewStub) v.findViewById(stubId);
245     }
246
247     /**
248      * Returns a view stub for the given view id.
249      */
250     public static ViewStub findViewStubById(Activity a, int stubId) {
251         return (ViewStub) a.findViewById(stubId);
252     }
253
254     /**
255      * Updates {@param transforms} to be the same size as {@param tasks}.
256      */
257     public static void matchTaskListSize(List<Task> tasks, List<TaskViewTransform> transforms) {
258         // We can reuse the task transforms where possible to reduce object allocation
259         int taskTransformCount = transforms.size();
260         int taskCount = tasks.size();
261         if (taskTransformCount < taskCount) {
262             // If there are less transforms than tasks, then add as many transforms as necessary
263             for (int i = taskTransformCount; i < taskCount; i++) {
264                 transforms.add(new TaskViewTransform());
265             }
266         } else if (taskTransformCount > taskCount) {
267             // If there are more transforms than tasks, then just subset the transform list
268             transforms.subList(taskCount, taskTransformCount).clear();
269         }
270     }
271
272     /**
273      * Used for debugging, converts DP to PX.
274      */
275     public static float dpToPx(Resources res, float dp) {
276         return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
277     }
278
279     /**
280      * Adds a trace event for debugging.
281      */
282     public static void addTraceEvent(String event) {
283         Trace.traceBegin(Trace.TRACE_TAG_VIEW, event);
284         Trace.traceEnd(Trace.TRACE_TAG_VIEW);
285     }
286
287     /**
288      * Returns the application configuration, which is independent of the activity's current
289      * configuration in multiwindow.
290      */
291     public static Configuration getAppConfiguration(Context context) {
292         return context.getApplicationContext().getResources().getConfiguration();
293     }
294
295     /**
296      * Returns a lightweight dump of a rect.
297      */
298     public static String dumpRect(Rect r) {
299         if (r == null) {
300             return "N:0,0-0,0";
301         }
302         return r.left + "," + r.top + "-" + r.right + "," + r.bottom;
303     }
304 }