1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *      http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14package android.databinding.tool;
15
16import android.databinding.tool.reflection.ModelAnalyzer;
17import android.databinding.tool.reflection.ModelClass;
18import android.databinding.tool.store.ResourceBundle;
19import android.databinding.tool.util.L;
20import android.databinding.tool.util.Preconditions;
21import android.databinding.tool.writer.BRWriter;
22import android.databinding.tool.writer.DataBinderWriter;
23import android.databinding.tool.writer.DynamicUtilWriter;
24import android.databinding.tool.writer.JavaFileWriter;
25
26import java.util.Set;
27
28/**
29 * Chef class for compiler.
30 *
31 * Different build systems can initiate a version of this to handle their work
32 */
33public class CompilerChef {
34    private static final String[] VERSION_CODES = {
35            "BASE",                 // 1
36            "BASE_1_1",             // 2
37            "CUPCAKE",              // 3
38            "DONUT",                // 4
39            "ECLAIR",               // 5
40            "ECLAIRE_0_1",          // 6
41            "ECLAIR_MR1",           // 7
42            "FROYO",                // 8
43            "GINGERBREAD",          // 9
44            "GINGERBREAD_MR1",      // 10
45            "HONEYCOMB",            // 11
46            "HONEYCOMB_MR1",        // 12
47            "HONEYCOMB_MR2",        // 13
48            "ICE_CREAM_SANDWICH",   // 14
49            "ICE_CREAM_SANDWICH_MR1",// 15
50            "JELLY_BEAN",           // 16
51            "JELLY_BEAN_MR1",       // 17
52            "JELLY_BEAN_MR2",       // 18
53            "KITKAT",               // 19
54            "KITKAT_WATCH",         // 20
55            "LOLLIPOP",             // 21
56            "LOLLIPOP_MR1",         // 22
57            "M",                    // 23
58    };
59    private JavaFileWriter mFileWriter;
60    private ResourceBundle mResourceBundle;
61    private DataBinder mDataBinder;
62
63    private CompilerChef() {
64    }
65
66    public static CompilerChef createChef(ResourceBundle bundle, JavaFileWriter fileWriter) {
67        CompilerChef chef = new CompilerChef();
68
69        chef.mResourceBundle = bundle;
70        chef.mFileWriter = fileWriter;
71        chef.mResourceBundle.validateMultiResLayouts();
72        return chef;
73    }
74
75    public ResourceBundle getResourceBundle() {
76        return mResourceBundle;
77    }
78
79    public void ensureDataBinder() {
80        if (mDataBinder == null) {
81            mDataBinder = new DataBinder(mResourceBundle);
82            mDataBinder.setFileWriter(mFileWriter);
83        }
84    }
85
86    public boolean hasAnythingToGenerate() {
87        L.d("checking if we have anything to generate. bundle size: %s",
88                mResourceBundle == null ? -1 : mResourceBundle.getLayoutBundles().size());
89        return mResourceBundle != null && mResourceBundle.getLayoutBundles().size() > 0;
90    }
91
92    public void writeDataBinderMapper(int minSdk, BRWriter brWriter) {
93        ensureDataBinder();
94        final String pkg = "android.databinding";
95        DataBinderWriter dbr = new DataBinderWriter(pkg, mResourceBundle.getAppPackage(),
96                "DataBinderMapper", mDataBinder.getLayoutBinders(), minSdk);
97        mFileWriter.writeToFile(pkg + "." + dbr.getClassName(), dbr.write(brWriter));
98    }
99
100    public void writeDynamicUtil() {
101        DynamicUtilWriter dynamicUtil = new DynamicUtilWriter();
102        // TODO: Replace this with targetSDK check from plugin
103        ModelClass versionCodes = ModelAnalyzer.getInstance().findClass(
104                "android.os.Build.VERSION_CODES", null);
105        Preconditions.checkNotNull(versionCodes, "Could not find compile SDK");
106        int compileVersion = 1;
107        for (int i = VERSION_CODES.length - 1; i >= 0; i--) {
108            if (versionCodes.findGetterOrField(VERSION_CODES[i], true) != null) {
109                compileVersion = i + 1;
110                break;
111            }
112        }
113        mFileWriter.writeToFile("android.databinding.DynamicUtil",
114                dynamicUtil.write(compileVersion).generate());
115    }
116
117    /**
118     * Adds variables to list of Bindables.
119     */
120    public void addBRVariables(BindableHolder bindables) {
121        ensureDataBinder();
122        for (LayoutBinder layoutBinder : mDataBinder.mLayoutBinders) {
123            for (String variableName : layoutBinder.getUserDefinedVariables().keySet()) {
124                bindables.addVariable(variableName, layoutBinder.getClassName());
125            }
126        }
127    }
128
129    public void sealModels() {
130        ensureDataBinder();
131        mDataBinder.sealModels();
132    }
133
134    public void writeViewBinderInterfaces(boolean isLibrary) {
135        ensureDataBinder();
136        mDataBinder.writerBaseClasses(isLibrary);
137    }
138
139    public void writeViewBinders(int minSdk) {
140        ensureDataBinder();
141        mDataBinder.writeBinders(minSdk);
142    }
143
144    public void writeComponent() {
145        ensureDataBinder();
146        mDataBinder.writeComponent();
147    }
148
149    public Set<String> getWrittenClassNames() {
150        ensureDataBinder();
151        return mDataBinder.getWrittenClassNames();
152    }
153
154    public interface BindableHolder {
155        void addVariable(String variableName, String containingClassName);
156    }
157}
158