1 /*
2  * Copyright (C) 2021 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 package androidx.constraintlayout.core.motion.utils;
17 
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.net.Socket;
21 import java.util.Arrays;
22 
23 public class Utils {
24     // @TODO: add description
log(String tag, String value)25     public static void log(String tag, String value) {
26         System.out.println(tag + " : " + value);
27     }
28 
29     // @TODO: add description
loge(String tag, String value)30     public static void loge(String tag, String value) {
31         System.err.println(tag + " : " + value);
32     }
33 
34     // @TODO: add description
socketSend(String str)35     public static void socketSend(String str) {
36         try {
37             Socket socket = new Socket("127.0.0.1", 5327);
38             OutputStream out = socket.getOutputStream();
39             out.write(str.getBytes());
40             out.close();
41         } catch (IOException e) {
42             //TODO replace with something not equal to printStackTrace();
43             System.err.println(e.toString()+"\n"+ Arrays.toString(e.getStackTrace())
44                     .replace("[","   at ")
45                     .replace(",","\n   at")
46                     .replace("]",""));
47         }
48     }
49 
clamp(int c)50     private static int clamp(int c) {
51         int n = 255;
52         c &= ~(c >> 31);
53         c -= n;
54         c &= (c >> 31);
55         c += n;
56         return c;
57     }
58 
59     // @TODO: add description
getInterpolatedColor(float[] value)60     public int getInterpolatedColor(float[] value) {
61         int r = clamp((int) ((float) Math.pow(value[0], 1.0 / 2.2) * 255.0f));
62         int g = clamp((int) ((float) Math.pow(value[1], 1.0 / 2.2) * 255.0f));
63         int b = clamp((int) ((float) Math.pow(value[2], 1.0 / 2.2) * 255.0f));
64         int a = clamp((int) (value[3] * 255.0f));
65         int color = (a << 24) | (r << 16) | (g << 8) | b;
66         return color;
67     }
68 
69     // @TODO: add description
rgbaTocColor(float r, float g, float b, float a)70     public static int rgbaTocColor(float r, float g, float b, float a) {
71         int ir = clamp((int) (r * 255f));
72         int ig = clamp((int) (g * 255f));
73         int ib = clamp((int) (b * 255f));
74         int ia = clamp((int) (a * 255f));
75         int color = (ia << 24) | (ir << 16) | (ig << 8) | ib;
76         return color;
77     }
78 
79     public interface DebugHandle {
80         // @TODO: add description
message(String str)81         void message(String str);
82     }
83 
84     static DebugHandle sOurHandle;
85 
setDebugHandle(DebugHandle handle)86     public static void setDebugHandle(DebugHandle handle) {
87         sOurHandle = handle;
88     }
89 
90     // @TODO: add description
logStack(String msg, int n)91     public static void logStack(String msg, int n) {
92         StackTraceElement[] st = new Throwable().getStackTrace();
93         String s = " ";
94         n = Math.min(n, st.length - 1);
95         for (int i = 1; i <= n; i++) {
96             StackTraceElement ste = st[i];
97             String stack = ".(" + ste.getFileName() + ":"
98                     + ste.getLineNumber() + ") " + ste.getMethodName();
99             s += " ";
100             System.out.println(msg + s + stack + s);
101         }
102     }
103 
104     // @TODO: add description
log(String str)105     public static void log(String str) {
106         StackTraceElement s = new Throwable().getStackTrace()[1];
107         String methodName = s.getMethodName();
108         methodName = (methodName + "                  ").substring(0, 17);
109         String npad = "    ".substring(Integer.toString(s.getLineNumber()).length());
110         String ss = ".(" + s.getFileName() + ":" + s.getLineNumber() + ")" + npad + methodName;
111         System.out.println(ss + " " + str);
112         if (sOurHandle != null) {
113             sOurHandle.message(ss + " " + str);
114         }
115     }
116 
117 }
118