This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
/
Utilities.java
160 lines (137 loc) · 5.8 KB
/
Utilities.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.recents.misc;
import android.animation.Animator;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.view.View;
import java.util.ArrayList;
/* Common code */
public class Utilities {
/** Scales a rect about its centroid */
public static void scaleRectAboutCenter(Rect r, float scale) {
if (scale != 1.0f) {
int cx = r.centerX();
int cy = r.centerY();
r.offset(-cx, -cy);
r.left = (int) (r.left * scale + 0.5f);
r.top = (int) (r.top * scale + 0.5f);
r.right = (int) (r.right * scale + 0.5f);
r.bottom = (int) (r.bottom * scale + 0.5f);
r.offset(cx, cy);
}
}
/** Maps a coorindate in a descendant view into the parent. */
public static float mapCoordInDescendentToSelf(View descendant, View root,
float[] coord, boolean includeRootScroll) {
ArrayList<View> ancestorChain = new ArrayList<View>();
float[] pt = {coord[0], coord[1]};
View v = descendant;
while(v != root && v != null) {
ancestorChain.add(v);
v = (View) v.getParent();
}
ancestorChain.add(root);
float scale = 1.0f;
int count = ancestorChain.size();
for (int i = 0; i < count; i++) {
View v0 = ancestorChain.get(i);
// For TextViews, scroll has a meaning which relates to the text position
// which is very strange... ignore the scroll.
if (v0 != descendant || includeRootScroll) {
pt[0] -= v0.getScrollX();
pt[1] -= v0.getScrollY();
}
v0.getMatrix().mapPoints(pt);
pt[0] += v0.getLeft();
pt[1] += v0.getTop();
scale *= v0.getScaleX();
}
coord[0] = pt[0];
coord[1] = pt[1];
return scale;
}
/** Maps a coordinate in the root to a descendent. */
public static float mapCoordInSelfToDescendent(View descendant, View root,
float[] coord, Matrix tmpInverseMatrix) {
ArrayList<View> ancestorChain = new ArrayList<View>();
float[] pt = {coord[0], coord[1]};
View v = descendant;
while(v != root) {
ancestorChain.add(v);
v = (View) v.getParent();
}
ancestorChain.add(root);
float scale = 1.0f;
int count = ancestorChain.size();
tmpInverseMatrix.set(Matrix.IDENTITY_MATRIX);
for (int i = count - 1; i >= 0; i--) {
View ancestor = ancestorChain.get(i);
View next = i > 0 ? ancestorChain.get(i-1) : null;
pt[0] += ancestor.getScrollX();
pt[1] += ancestor.getScrollY();
if (next != null) {
pt[0] -= next.getLeft();
pt[1] -= next.getTop();
next.getMatrix().invert(tmpInverseMatrix);
tmpInverseMatrix.mapPoints(pt);
scale *= next.getScaleX();
}
}
coord[0] = pt[0];
coord[1] = pt[1];
return scale;
}
/** Calculates the constrast between two colors, using the algorithm provided by the WCAG v2. */
public static float computeContrastBetweenColors(int bg, int fg) {
float bgR = Color.red(bg) / 255f;
float bgG = Color.green(bg) / 255f;
float bgB = Color.blue(bg) / 255f;
bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
float fgR = Color.red(fg) / 255f;
float fgG = Color.green(fg) / 255f;
float fgB = Color.blue(fg) / 255f;
fgR = (fgR < 0.03928f) ? fgR / 12.92f : (float) Math.pow((fgR + 0.055f) / 1.055f, 2.4f);
fgG = (fgG < 0.03928f) ? fgG / 12.92f : (float) Math.pow((fgG + 0.055f) / 1.055f, 2.4f);
fgB = (fgB < 0.03928f) ? fgB / 12.92f : (float) Math.pow((fgB + 0.055f) / 1.055f, 2.4f);
float fgL = 0.2126f * fgR + 0.7152f * fgG + 0.0722f * fgB;
return Math.abs((fgL + 0.05f) / (bgL + 0.05f));
}
/** Returns the base color overlaid with another overlay color with a specified alpha. */
public static int getColorWithOverlay(int baseColor, int overlayColor, float overlayAlpha) {
return Color.rgb(
(int) (overlayAlpha * Color.red(baseColor) +
(1f - overlayAlpha) * Color.red(overlayColor)),
(int) (overlayAlpha * Color.green(baseColor) +
(1f - overlayAlpha) * Color.green(overlayColor)),
(int) (overlayAlpha * Color.blue(baseColor) +
(1f - overlayAlpha) * Color.blue(overlayColor)));
}
/**
* Cancels an animation ensuring that if it has listeners, onCancel and onEnd
* are not called.
*/
public static void cancelAnimationWithoutCallbacks(Animator animator) {
if (animator != null) {
animator.removeAllListeners();
animator.cancel();
}
}
}