Virtual Trading v2.0.95版本的 MD5 值为:ef0f95d706f7364286da0edbbd29a27d

以下内容为反编译后的 DynamicComponents.java 源代码,内容仅作参考


package com.yusufcihan.DynamicComponents;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.ComponentDescriptorConstants;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.errors.YailRuntimeError;
import com.google.appinventor.components.runtime.util.YailDictionary;
import com.google.appinventor.components.runtime.util.YailList;
import com.onesignal.outcomes.OSOutcomeConstants;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@DesignerComponent(category = ComponentCategory.EXTENSION, description = "Dynamic Components is an extension that creates any component in your App Inventor distribution programmatically, instead of having pre-defined components. Made with ❤️ by Yusuf Cihan.", helpUrl = "https://github.com/ysfchn/DynamicComponents-AI2/blob/main/README.md", iconName = "aiwebres/icon.png", nonVisible = true, version = 9, versionName = "2.2.2")
@SimpleObject(external = true)
public class DynamicComponents extends AndroidNonvisibleComponent {
    private final String BASE;
    private final HashMap<String, Component> COMPONENTS;
    private final HashMap<Component, String> COMPONENT_IDS;
    private final Util UTIL_INSTANCE;
    private ArrayList<ComponentListener> componentListeners;
    private Object lastUsedId;
    private boolean postOnUiThread;
    private JSONArray propertiesArray;

    public interface ComponentListener {
        void onCreation(Component component, String str);
    }

    protected class Internal {
        public Internal() {
        }

        public String checkId(Object obj, String str) {
            if (str == null || str.trim().isEmpty()) {
                throw new YailRuntimeError("DynamicComponents-AI2: ID can't be blank.", "Invalid ID");
            }
            if (DynamicComponents.this.COMPONENTS.isIdTaken(str)) {
                throw new YailRuntimeError("DynamicComponents-AI2: ID must be unique for all components.", "Duplicate ID");
            }
            return DynamicComponents.this.COMPONENTS.getClassName(obj);
        }

        public void createBySchema(AndroidViewComponent androidViewComponent, int i, JSONArray jSONArray) {
            if (!jSONArray.getJSONObject(i).has("in")) {
                DynamicComponents.this.Create(androidViewComponent, jSONArray.getJSONObject(i).getString("type"), jSONArray.getJSONObject(i).getString(OSOutcomeConstants.OUTCOME_ID));
            } else {
                DynamicComponents dynamicComponents = DynamicComponents.this;
                dynamicComponents.Create((AndroidViewComponent) dynamicComponents.GetComponent(jSONArray.getJSONObject(i).getString("in")), jSONArray.getJSONObject(i).getString("type"), jSONArray.getJSONObject(i).getString(OSOutcomeConstants.OUTCOME_ID));
            }
        }

        public Method findMethod(Method[] methodArr, String str, Integer num) {
            for (Method method : methodArr) {
                if (method.getName().equals(str.trim()) && method.getParameterTypes().length == num.intValue()) {
                    return method;
                }
            }
            return null;
        }

        public <T extends Annotation> T getAnnotation(Class<T> cls, Component component, Method method) {
            return component != null ? (T) DynamicComponents.this.COMPONENTS.getClass(component).getAnnotation(cls) : (T) method.getAnnotation(cls);
        }

        public Class<?> getClass(Object obj) {
            return obj.getClass();
        }

        public String getClassName(Object obj) {
            boolean z = obj instanceof String;
            if (z && obj.toString().contains(".")) {
                return obj.toString().replace(" ", "");
            }
            if (!z) {
                if (obj instanceof Component) {
                    return obj.getClass().getName();
                }
                throw new YailRuntimeError("DynamicComponents-AI2: Not a Component block or a String.", "Invalid Component");
            }
            return "com.google.appinventor.components.runtime." + obj.toString().replace(" ", "");
        }

        public boolean isDynamicComponent(Component component) {
            return DynamicComponents.this.COMPONENT_IDS.containsValue(component);
        }

        public boolean isIdTaken(String str) {
            return DynamicComponents.this.COMPONENT_IDS.containsKey(str);
        }

        public String loopReplace(JSONObject jSONObject, YailList yailList, String str) {
            for (int i = 0; i < jSONObject.optJSONArray("keys").length(); i++) {
                str = str.replace("{" + jSONObject.optJSONArray("keys").getString(i) + "}", yailList.getString(i).replace("\"", ""));
            }
            return str;
        }

        public boolean methodHasAnnotation(Class<? extends Annotation> cls, Method method) {
            return method.isAnnotationPresent(cls);
        }

        public void parseJson(String str, JSONObject jSONObject) throws JSONException {
            JSONObject jSONObject2 = new JSONObject(jSONObject.toString());
            jSONObject2.remove("components");
            if (!"".equals(str)) {
                jSONObject2.put("in", str);
            }
            DynamicComponents.this.propertiesArray.put(jSONObject2);
            if (jSONObject.has("components")) {
                for (int i = 0; i < jSONObject.getJSONArray("components").length(); i++) {
                    parseJson(jSONObject2.optString(OSOutcomeConstants.OUTCOME_ID, ""), jSONObject.getJSONArray("components").getJSONObject(i));
                }
            }
        }

        public void putInJsonArray(JSONArray jSONArray, Object obj) {
            try {
                jSONArray.put(obj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        public void putInJsonObject(JSONObject jSONObject, String str, Object obj) {
            try {
                jSONObject.put(str, obj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        public String replaceKeys(JSONObject jSONObject, YailList yailList, String str) {
            if (jSONObject.has("keys")) {
                if (jSONObject.optJSONArray("keys").length() > yailList.length()) {
                    throw new YailRuntimeError("Input parameter count is lower than the requirement!", "Error");
                }
                loopReplace(jSONObject, yailList, str);
            }
            return str;
        }

        public void setPropertiesBySchema(int i, JSONArray jSONArray) {
            JSONArray names = jSONArray.getJSONObject(i).getJSONObject("properties").names();
            for (int i2 = 0; i2 < names.length(); i2++) {
                DynamicComponents dynamicComponents = DynamicComponents.this;
                dynamicComponents.Invoke((Component) dynamicComponents.GetComponent(jSONArray.getJSONObject(i).getString(OSOutcomeConstants.OUTCOME_ID)), names.getString(i2), YailList.makeList(new Object[]{jSONArray.getJSONObject(i).getJSONObject("properties").get(names.getString(i2))}));
            }
        }
    }

    public class Util {
        Util() {
        }

        public boolean exists(Component component) {
            return DynamicComponents.this.COMPONENTS.containsValue(component);
        }

        public boolean exists(String str) {
            return DynamicComponents.this.COMPONENTS.containsKey(str);
        }

        public String getClassName(Object obj) {
            String replaceAll = obj.toString().replaceAll("[^.$@a-zA-Z0-9]", "");
            boolean z = obj instanceof String;
            if (z && replaceAll.contains(".")) {
                return replaceAll;
            }
            if (!z) {
                if (obj instanceof Component) {
                    return obj.getClass().getName().replaceAll("[^.$@a-zA-Z0-9]", "");
                }
                throw new YailRuntimeError("Component is invalid.", "DynamicComponents");
            }
            return "com.google.appinventor.components.runtime." + replaceAll;
        }

        public Method getMethod(Method[] methodArr, String str, int i) {
            String replaceAll = str.replaceAll("[^a-zA-Z0-9]", "");
            for (Method method : methodArr) {
                int length = method.getParameterTypes().length;
                if (method.getName().equals(replaceAll) && length == i) {
                    return method;
                }
            }
            return null;
        }

        public void newInstance(Constructor<?> constructor, String str, AndroidViewComponent androidViewComponent) {
            try {
                try {
                    Component component = (Component) constructor.newInstance((ComponentContainer) androidViewComponent);
                    if (DynamicComponents.this.isEmptyOrNull(component)) {
                        return;
                    }
                    String simpleName = component.getClass().getSimpleName();
                    if (simpleName == "ImageSprite" || simpleName == "Sprite") {
                        DynamicComponents.this.Invoke(component, "Initialize", new YailList());
                    }
                    DynamicComponents.this.COMPONENT_IDS.put(component, str);
                    DynamicComponents.this.COMPONENTS.put(str, component);
                    notifyListenersOfCreation(component, str);
                    DynamicComponents.this.ComponentBuilt(component, str, simpleName);
                } catch (Exception e) {
                    throw new YailRuntimeError(e.getMessage(), "DynamicComponents");
                }
            } catch (Throwable th) {
                Object obj = null;
                if (!DynamicComponents.this.isEmptyOrNull(null)) {
                    String simpleName2 = obj.getClass().getSimpleName();
                    if (simpleName2 == "ImageSprite" || simpleName2 == "Sprite") {
                        DynamicComponents.this.Invoke(null, "Initialize", new YailList());
                    }
                    DynamicComponents.this.COMPONENT_IDS.put(null, str);
                    DynamicComponents.this.COMPONENTS.put(str, null);
                    notifyListenersOfCreation(null, str);
                    DynamicComponents.this.ComponentBuilt(null, str, simpleName2);
                }
                throw th;
            }
        }

        public void notifyListenersOfCreation(Component component, String str) {
            Iterator it = DynamicComponents.this.componentListeners.iterator();
            while (it.hasNext()) {
                ((ComponentListener) it.next()).onCreation(component, str);
            }
        }

        public void parse(String str, JSONObject jSONObject) {
            JSONObject jSONObject2 = new JSONObject(jSONObject.toString());
            jSONObject2.remove("components");
            if (!"".equals(str)) {
                jSONObject2.put("in", str);
            }
            DynamicComponents.this.propertiesArray.put(jSONObject2);
            if (jSONObject.has("components")) {
                for (int i = 0; i < jSONObject.getJSONArray("components").length(); i++) {
                    parse(jSONObject2.optString(OSOutcomeConstants.OUTCOME_ID, ""), jSONObject.getJSONArray("components").getJSONObject(i));
                }
            }
        }
    }

    public DynamicComponents(ComponentContainer componentContainer) {
        super(componentContainer.$form());
        this.BASE = "com.google.appinventor.components.runtime.";
        this.postOnUiThread = false;
        this.COMPONENTS = new HashMap<>();
        this.COMPONENT_IDS = new HashMap<>();
        this.lastUsedId = "";
        this.componentListeners = new ArrayList<>();
        this.propertiesArray = new JSONArray();
        this.UTIL_INSTANCE = new Util();
    }

    @SimpleFunction(description = "Assign a new ID to a previously created dynamic component.")
    public void ChangeId(String str, String str2) {
        if (!this.UTIL_INSTANCE.exists(str) || this.UTIL_INSTANCE.exists(str2)) {
            throw new YailRuntimeError("The ID you used is either not a dynamic component, or the ID you've used to replace the old ID is already taken.", "DynamicComponents");
        }
        for (String str3 : UsedIDs().toStringArray()) {
            if (str3.contains(str)) {
                Component component = (Component) GetComponent(str3);
                String replace = str3.replace(str, str2);
                this.COMPONENT_IDS.remove(component);
                HashMap<String, Component> hashMap = this.COMPONENTS;
                hashMap.put(replace, hashMap.remove(str3));
                this.COMPONENT_IDS.put(component, replace);
            }
        }
    }

    @SimpleEvent(description = "Is called after a component has been created.")
    public void ComponentBuilt(final Component component, final String str, final String str2) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                EventDispatcher.dispatchEvent(DynamicComponents.this, "ComponentBuilt", component, str, str2);
            }
        });
    }

    @SimpleFunction(description = "Creates a new dynamic component.")
    public void Create(final AndroidViewComponent androidViewComponent, Object obj, final String str) throws Exception {
        if (this.COMPONENTS.containsKey(str)) {
            throw new YailRuntimeError("Expected a unique ID, got '" + str + "'.", "DynamicComponents");
        }
        this.lastUsedId = str;
        final Constructor<?> constructor = Class.forName(this.UTIL_INSTANCE.getClassName(obj)).getConstructor(ComponentContainer.class);
        if (this.postOnUiThread) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    DynamicComponents.this.UTIL_INSTANCE.newInstance(constructor, str, androidViewComponent);
                }
            });
        } else {
            this.UTIL_INSTANCE.newInstance(constructor, str, androidViewComponent);
        }
    }

    @SimpleFunction(description = "Generates a random ID to create a component with.")
    public String GenerateID() {
        String uuid;
        do {
            uuid = UUID.randomUUID().toString();
        } while (this.UTIL_INSTANCE.exists(uuid));
        return uuid;
    }

    @SimpleFunction(description = "Returns the component associated with the specified ID.")
    public Object GetComponent(String str) {
        return this.COMPONENTS.get(str);
    }

    @SimpleFunction(description = "Get meta data about the specified component.")
    public YailDictionary GetComponentMeta(Component component) {
        Class<?> cls = component.getClass();
        DesignerComponent designerComponent = (DesignerComponent) cls.getAnnotation(DesignerComponent.class);
        boolean z = !isEmptyOrNull(designerComponent);
        SimpleObject simpleObject = (SimpleObject) cls.getAnnotation(SimpleObject.class);
        YailDictionary yailDictionary = new YailDictionary();
        boolean z2 = !isEmptyOrNull(simpleObject);
        if (z && z2) {
            yailDictionary.put(ComponentDescriptorConstants.ANDROIDMINSDK_TARGET, Integer.valueOf(designerComponent.androidMinSdk()));
            yailDictionary.put("category", designerComponent.category());
            yailDictionary.put("dateBuilt", designerComponent.dateBuilt());
            yailDictionary.put("description", designerComponent.description());
            yailDictionary.put("designerHelpDescription", designerComponent.designerHelpDescription());
            yailDictionary.put("external", Boolean.valueOf(simpleObject.external()));
            yailDictionary.put("helpUrl", designerComponent.helpUrl());
            yailDictionary.put("iconName", designerComponent.iconName());
            yailDictionary.put("nonVisible", Boolean.valueOf(designerComponent.nonVisible()));
            yailDictionary.put("package", cls.getName());
            yailDictionary.put("showOnPalette", Boolean.valueOf(designerComponent.showOnPalette()));
            yailDictionary.put("type", cls.getSimpleName());
            yailDictionary.put("version", Integer.valueOf(designerComponent.version()));
            yailDictionary.put("versionName", designerComponent.versionName());
        } else {
            if (!z && z2) {
                yailDictionary.put("external", Boolean.valueOf(simpleObject.external()));
            }
            yailDictionary.put("package", cls.getName());
            yailDictionary.put("type", cls.getSimpleName());
        }
        return yailDictionary;
    }

    @SimpleFunction(description = "Get meta data about events for the specified component.")
    public YailDictionary GetEventMeta(Component component) {
        Method[] methods = component.getClass().getMethods();
        YailDictionary yailDictionary = new YailDictionary();
        for (Method method : methods) {
            SimpleEvent simpleEvent = (SimpleEvent) method.getAnnotation(SimpleEvent.class);
            boolean z = !isEmptyOrNull(method.getAnnotation(Deprecated.class));
            String name = method.getName();
            YailDictionary yailDictionary2 = new YailDictionary();
            if (isEmptyOrNull(simpleEvent)) {
                yailDictionary2.put("isDeprecated", Boolean.valueOf(z));
            } else {
                yailDictionary2.put("description", simpleEvent.description());
                yailDictionary2.put("isDeprecated", Boolean.valueOf(z));
                yailDictionary2.put("userVisible", Boolean.valueOf(simpleEvent.userVisible()));
            }
            yailDictionary.put(name, yailDictionary2);
        }
        return yailDictionary;
    }

    @SimpleFunction(description = "Get meta data about functions for the specified component.")
    public YailDictionary GetFunctionMeta(Component component) {
        Method[] methods = component.getClass().getMethods();
        YailDictionary yailDictionary = new YailDictionary();
        for (Method method : methods) {
            SimpleFunction simpleFunction = (SimpleFunction) method.getAnnotation(SimpleFunction.class);
            boolean z = !isEmptyOrNull(method.getAnnotation(Deprecated.class));
            String name = method.getName();
            YailDictionary yailDictionary2 = new YailDictionary();
            if (isEmptyOrNull(simpleFunction)) {
                yailDictionary2.put("isDeprecated", Boolean.valueOf(z));
            } else {
                yailDictionary2.put("description", simpleFunction.description());
                yailDictionary2.put("isDeprecated", Boolean.valueOf(z));
                yailDictionary2.put("userVisible", Boolean.valueOf(simpleFunction.userVisible()));
            }
            yailDictionary.put(name, yailDictionary2);
        }
        return yailDictionary;
    }

    @SimpleFunction(description = "Returns the ID of the specified component.")
    public String GetId(Component component) {
        return (!isEmptyOrNull(component) || this.COMPONENT_IDS.containsKey(component)) ? this.COMPONENT_IDS.get(component) : "";
    }

    @SimpleFunction(description = "Do NOT use this function. Use 'GetComponentMeta' as a replacement.")
    @Deprecated
    public String GetName(Component component) {
        return component.getClass().getName();
    }

    @SimpleFunction(description = "Returns the position of the specified component according to its parent view. Index begins at one.")
    public int GetOrder(AndroidViewComponent androidViewComponent) {
        View view = androidViewComponent.getView();
        ViewGroup viewGroup = !isEmptyOrNull(view) ? (ViewGroup) view.getParent() : null;
        if (isEmptyOrNull(view) || isEmptyOrNull(viewGroup)) {
            return 0;
        }
        return viewGroup.indexOfChild(view) + 1;
    }

    @SimpleFunction(description = "Get a properties value.")
    public Object GetProperty(Component component, String str) {
        return Invoke(component, str, YailList.makeEmptyList());
    }

    @SimpleFunction(description = "Get meta data about properties for the specified component, including their values.")
    public YailDictionary GetPropertyMeta(Component component) {
        Method[] methods = component.getClass().getMethods();
        YailDictionary yailDictionary = new YailDictionary();
        for (Method method : methods) {
            DesignerProperty designerProperty = (DesignerProperty) method.getAnnotation(DesignerProperty.class);
            boolean z = !isEmptyOrNull(designerProperty);
            SimpleProperty simpleProperty = (SimpleProperty) method.getAnnotation(SimpleProperty.class);
            String name = method.getName();
            YailDictionary yailDictionary2 = new YailDictionary();
            Object Invoke = Invoke(component, name, new YailList());
            if (!isEmptyOrNull(simpleProperty)) {
                yailDictionary2.put("description", simpleProperty.description());
                yailDictionary2.put("category", simpleProperty.category());
                if (z) {
                    YailDictionary yailDictionary3 = new YailDictionary();
                    yailDictionary3.put("defaultValue", designerProperty.defaultValue());
                    yailDictionary3.put("editorArgs", designerProperty.editorArgs());
                    yailDictionary3.put("editorType", designerProperty.editorType());
                    yailDictionary2.put("designer", yailDictionary3);
                }
                yailDictionary2.put("isDeprecated", Boolean.valueOf(!isEmptyOrNull(method.getAnnotation(Deprecated.class))));
                yailDictionary2.put("isDesignerProperty", Boolean.valueOf(z));
                yailDictionary2.put("userVisible", Boolean.valueOf(simpleProperty.userVisible()));
                yailDictionary2.put("value", Invoke);
                yailDictionary.put(name, yailDictionary2);
            }
        }
        return yailDictionary;
    }

    @SimpleFunction(description = "Invokes a method with parameters.")
    public Object Invoke(Component component, String str, YailList yailList) {
        if (isEmptyOrNull(component)) {
            throw new YailRuntimeError("Component cannot be null.", "DynamicComponents");
        }
        Method[] methods = component.getClass().getMethods();
        try {
            try {
                Object[] array = yailList.toArray();
                Method method = this.UTIL_INSTANCE.getMethod(methods, str, array.length);
                Class<?>[] parameterTypes = method.getParameterTypes();
                ArrayList arrayList = new ArrayList();
                for (int i = 0; i < parameterTypes.length; i++) {
                    arrayList.add("int".equals(parameterTypes[i].getName()) ? Integer.valueOf(Integer.parseInt(array[i].toString())) : "float".equals(parameterTypes[i].getName()) ? Float.valueOf(Float.parseFloat(array[i].toString())) : "double".equals(parameterTypes[i].getName()) ? Double.valueOf(Double.parseDouble(array[i].toString())) : "java.lang.String".equals(parameterTypes[i].getName()) ? array[i].toString() : "boolean".equals(parameterTypes[i].getName()) ? Boolean.valueOf(Boolean.parseBoolean(array[i].toString())) : array[i]);
                }
                Object invoke = method.invoke(component, arrayList.toArray());
                return !isEmptyOrNull(invoke) ? invoke : "";
            } catch (Exception e) {
                throw new YailRuntimeError(e.getMessage(), "DynamicComponents");
            }
        } catch (Throwable unused) {
            if (isEmptyOrNull(null)) {
                return "";
            }
            return null;
        }
    }

    @SimpleFunction(description = "Returns if the specified component was created by the Dynamic Components extension.")
    public boolean IsDynamic(Component component) {
        return this.COMPONENTS.containsValue(component);
    }

    @SimpleFunction(description = "Returns the last used ID.")
    public Object LastUsedID() {
        return this.lastUsedId;
    }

    @SimpleFunction(description = "Do NOT use this function. Use 'GetComponentMeta', 'GetEventMeta', 'GetFunctionMeta', and 'GetPropertyMeta' as replacements.")
    @Deprecated
    public String ListDetails(Component component) {
        return "";
    }

    @SimpleFunction(description = "Moves the specified component to the specified view.")
    public void Move(AndroidViewComponent androidViewComponent, AndroidViewComponent androidViewComponent2) {
        View view = androidViewComponent2.getView();
        (!isEmptyOrNull(view) ? (ViewGroup) view.getParent() : null).removeView(view);
        ((ViewGroup) ((ViewGroup) androidViewComponent.getView()).getChildAt(0)).addView(view);
    }

    @SimpleFunction(description = "Do NOT use this function. Use 'GenerateID' as a replacement.")
    @Deprecated
    public String RandomUUID() {
        return GenerateID();
    }

    @SimpleFunction(description = "Removes the component with the specified ID from the layout/screen so the ID can be reused.")
    public void Remove(String str) {
        Component component = this.COMPONENTS.get(str);
        if (isEmptyOrNull(component)) {
            return;
        }
        try {
            final View view = (View) component.getClass().getMethod("getView", new Class[0]).invoke(component, new Object[0]);
            final ViewGroup viewGroup = (ViewGroup) view.getParent();
            if (this.postOnUiThread) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        viewGroup.removeView(view);
                    }
                });
            } else {
                viewGroup.removeView(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.COMPONENTS.remove(str);
        this.COMPONENT_IDS.remove(component);
    }

    @SimpleFunction(description = "Uses a JSON Object to create dynamic components. Templates can also contain parameters that will be replaced with the values which are defined from the parameters list.")
    public void Schema(AndroidViewComponent androidViewComponent, String str, YailList yailList) throws Exception {
        JSONObject jSONObject = new JSONObject(str);
        if (isEmptyOrNull(str) || !jSONObject.has("components")) {
            throw new YailRuntimeError("The template is empty, or is does not have any components.", "DynamicComponents");
        }
        this.propertiesArray = new JSONArray();
        JSONArray jSONArray = jSONObject.has("keys") ? jSONObject.getJSONArray("keys") : null;
        if (!isEmptyOrNull(jSONArray) && jSONArray.length() == yailList.length() - 1) {
            for (int i = 0; i < jSONArray.length(); i++) {
                String str2 = "%" + jSONArray.getString(i);
                String str3 = "{" + jSONArray.getString(i) + "}";
                String replace = yailList.getString(i).replace("\"", "");
                str = str.replace(str2, replace).replace(str3, replace);
            }
        }
        JSONObject jSONObject2 = new JSONObject(str);
        this.UTIL_INSTANCE.parse("", jSONObject2);
        this.propertiesArray.remove(0);
        for (int i2 = 0; i2 < this.propertiesArray.length(); i2++) {
            if (!this.propertiesArray.getJSONObject(i2).has(OSOutcomeConstants.OUTCOME_ID)) {
                throw new YailRuntimeError("One or multiple components do not have a specified ID in the template.", "DynamicComponents");
            }
            final JSONObject jSONObject3 = this.propertiesArray.getJSONObject(i2);
            final String string = jSONObject3.getString(OSOutcomeConstants.OUTCOME_ID);
            AndroidViewComponent androidViewComponent2 = !jSONObject3.has("in") ? androidViewComponent : (AndroidViewComponent) GetComponent(jSONObject3.getString("in"));
            Object string2 = jSONObject3.getString("type");
            this.componentListeners.add(new ComponentListener() {
                @Override
                public void onCreation(Component component, String str4) {
                    if (str4 == string && jSONObject3.has("properties")) {
                        JSONObject jSONObject4 = jSONObject3.getJSONObject("properties");
                        JSONArray names = jSONObject4.names();
                        for (int i3 = 0; i3 < names.length(); i3++) {
                            DynamicComponents dynamicComponents = DynamicComponents.this;
                            dynamicComponents.Invoke((Component) dynamicComponents.GetComponent(string), names.getString(i3), YailList.makeList(new Object[]{jSONObject4.get(names.getString(i3))}));
                        }
                        DynamicComponents.this.componentListeners.remove(this);
                    }
                }
            });
            Create(androidViewComponent2, string2, string);
        }
        SchemaCreated(jSONObject2.getString("name"), yailList);
    }

    @SimpleEvent(description = "Is called after a schema has/mostly finished component creation.")
    public void SchemaCreated(final String str, final YailList yailList) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                EventDispatcher.dispatchEvent(DynamicComponents.this, "SchemaCreated", str, yailList);
            }
        });
    }

    @SimpleFunction(description = "Sets the order of the specified component according to its parent view. Typing zero will move the component to the end, index begins at one.")
    public void SetOrder(AndroidViewComponent androidViewComponent, int i) {
        int i2 = i - 1;
        View view = androidViewComponent.getView();
        ViewGroup viewGroup = (ViewGroup) view.getParent();
        viewGroup.removeView(view);
        int childCount = viewGroup.getChildCount();
        if (i2 > childCount) {
            i2 = childCount;
        }
        viewGroup.addView(view, i2);
    }

    @SimpleFunction(description = "Set multiple properties of the specified component using a dictionary, including those only available from the Designer.")
    public void SetProperties(Component component, YailDictionary yailDictionary) throws Exception {
        JSONObject jSONObject = new JSONObject(yailDictionary.toString());
        JSONArray names = jSONObject.names();
        for (int i = 0; i < jSONObject.length(); i++) {
            String string = names.getString(i);
            Invoke(component, string, YailList.makeList(new Object[]{jSONObject.get(string)}));
        }
    }

    @SimpleFunction(description = "Set a property of the specified component, including those only available from the Designer.")
    public void SetProperty(Component component, String str, Object obj) {
        Invoke(component, str, YailList.makeList(new Object[]{obj}));
    }

    @SimpleProperty(userVisible = false)
    @DesignerProperty(defaultValue = "UI", editorArgs = {"Main", "UI"}, editorType = PropertyTypeConstants.PROPERTY_TYPE_CHOICES)
    public void Thread(String str) {
        this.postOnUiThread = str == "UI";
    }

    @SimpleFunction(description = "Returns all IDs of components created with the Dynamic Components extension.")
    public YailList UsedIDs() {
        return YailList.makeList((Set) this.COMPONENTS.keySet());
    }

    @SimpleProperty(description = "Returns the version of the Dynamic Components extension.")
    public int Version() {
        return ((DesignerComponent) DynamicComponents.class.getAnnotation(DesignerComponent.class)).version();
    }

    @SimpleProperty(description = "Returns the version name of the Dynamic Components extension.")
    public String VersionName() {
        return ((DesignerComponent) DynamicComponents.class.getAnnotation(DesignerComponent.class)).versionName();
    }

    public boolean isEmptyOrNull(Object obj) {
        return obj instanceof String ? obj.toString().replace(" ", "").isEmpty() : obj == null;
    }
}