Debian Codegraph @

compiler/src/main/kotlin/android/databinding/tool/writer/DynamicUtilWriter.kt

raw
1 package android.databinding.tool.writer;
2
3 class DynamicUtilWriter() {
4 public fun write(targetSdk : kotlin.Int) : KCode = kcode("package android.databinding;") {
5 nl("")
6 nl("import android.os.Build.VERSION;")
7 nl("import android.os.Build.VERSION_CODES;")
8 nl("")
9 block("public class DynamicUtil") {
10 nl("@SuppressWarnings(\"deprecation\")")
11 block("public static int getColorFromResource(final android.view.View view, final int resourceId)") {
12 if (targetSdk >= 23) {
13 block("if (VERSION.SDK_INT >= VERSION_CODES.M)") {
14 nl("return view.getContext().getColor(resourceId);")
15 }
16 }
17 nl("return view.getResources().getColor(resourceId);")
18 }
19
20 nl("@SuppressWarnings(\"deprecation\")")
21 block("public static android.content.res.ColorStateList getColorStateListFromResource(final android.view.View view, final int resourceId)") {
22 if (targetSdk >= 23) {
23 block("if (VERSION.SDK_INT >= VERSION_CODES.M)") {
24 nl("return view.getContext().getColorStateList(resourceId);")
25 }
26 }
27 nl("return view.getResources().getColorStateList(resourceId);")
28 }
29
30 nl("@SuppressWarnings(\"deprecation\")")
31 block("public static android.graphics.drawable.Drawable getDrawableFromResource(final android.view.View view, final int resourceId)") {
32 if (targetSdk >= 21) {
33 block("if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP)") {
34 nl("return view.getContext().getDrawable(resourceId);")
35 }
36 }
37 nl("return view.getResources().getDrawable(resourceId);")
38 }
39
40 block("public static boolean parse(String str, boolean fallback)") {
41 block("if (str == null)") {
42 nl("return fallback;");
43 }
44 nl("return Boolean.parseBoolean(str);")
45 }
46 block("public static byte parse(String str, byte fallback)") {
47 block("try") {
48 nl("return Byte.parseByte(str);")
49 }
50 block("catch (NumberFormatException e)") {
51 nl("return fallback;")
52 }
53 }
54 block("public static short parse(String str, short fallback)") {
55 block("try") {
56 nl("return Short.parseShort(str);")
57 }
58 block("catch (NumberFormatException e)") {
59 nl("return fallback;")
60 }
61 }
62 block("public static int parse(String str, int fallback)") {
63 block("try") {
64 nl("return Integer.parseInt(str);")
65 }
66 block("catch (NumberFormatException e)") {
67 nl("return fallback;")
68 }
69 }
70 block("public static long parse(String str, long fallback)") {
71 block("try") {
72 nl("return Long.parseLong(str);")
73 }
74 block("catch (NumberFormatException e)") {
75 nl("return fallback;")
76 }
77 }
78 block("public static float parse(String str, float fallback)") {
79 block("try") {
80 nl("return Float.parseFloat(str);")
81 }
82 block("catch (NumberFormatException e)") {
83 nl("return fallback;")
84 }
85 }
86 block("public static double parse(String str, double fallback)") {
87 block("try") {
88 nl("return Double.parseDouble(str);")
89 }
90 block("catch (NumberFormatException e)") {
91 nl("return fallback;")
92 }
93 }
94 block("public static char parse(String str, char fallback)") {
95 block ("if (str == null || str.isEmpty())") {
96 nl("return fallback;")
97 }
98 nl("return str.charAt(0);")
99 }
100 }
101 }
102 }