DynamicUtilWriter.kt revision 83a8c9d56cba07985d611ce7df47346b94742336
1package android.databinding.tool.writer;
2
3class DynamicUtilWriter() {
4    public fun write(targetSdk : kotlin.Int) : KCode = kcode("package android.databinding;") {
5        nl("")
6        nl("import android.content.res.ColorStateList;")
7        nl("import android.graphics.drawable.Drawable;")
8        nl("import android.os.Build.VERSION;")
9        nl("import android.os.Build.VERSION_CODES;")
10        nl("import android.view.View;")
11        nl("")
12        nl("public class DynamicUtil {")
13        tab("@SuppressWarnings(\"deprecation\")") {
14            nl("public static ColorStateList getColorStateListFromResource(View root, int resourceId) {")
15            if (targetSdk >= 23) {
16                tab("if (VERSION.SDK_INT >= VERSION_CODES.M) {") {
17                    tab("return root.getContext().getColorStateList(resourceId);")
18                }
19                tab("}")
20            }
21            tab("return root.getResources().getColorStateList(resourceId);")
22        }
23        tab("}")
24        nl("")
25
26        tab("@SuppressWarnings(\"deprecation\")") {
27            nl("public static Drawable getDrawableFromResource(View root, int resourceId) {")
28            if (targetSdk >= 21) {
29                tab("if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {") {
30                    tab("return root.getContext().getDrawable(resourceId);")
31                }
32                tab("}")
33            }
34            tab("return root.getResources().getDrawable(resourceId);")
35        }
36        tab("}")
37        nl("}")
38    }
39}