diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 5764c8b..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,19 +0,0 @@ -on: push -name: Build my extension -jobs: - buildAIX: - name: Build AIX - runs-on: ubuntu-18.04 - steps: - - uses: actions/checkout@master - - name: Build AIX - id: build_aix - uses: ysfchn/appinventor-aix-action@master - with: - source: 'https://github.com/ysfchn/appinventor-sources.git' - - name: Upload Artifact - id: upload-artifact - uses: actions/upload-artifact@v1.0.0 - with: - name: ${{ steps.build_aix.outputs.file }} - path: appinventor-sources/appinventor/components/build/extensions/${{ steps.build_aix.outputs.file }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0519d90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/* +out/* \ No newline at end of file diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..bfd61c9 --- /dev/null +++ b/build.xml @@ -0,0 +1,203 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Generated build file AndroidRuntime.jar [${extensionClassFolder}] + + + + + + + + + + + + + + + + + + + + + + + Dexing extension: ${extensionType} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/android/android.jar b/lib/android/android.jar new file mode 100644 index 0000000..66e18e0 Binary files /dev/null and b/lib/android/android.jar differ diff --git a/lib/android/appcompat-v7-28.0.0.jar b/lib/android/appcompat-v7-28.0.0.jar new file mode 100644 index 0000000..a69b1ec Binary files /dev/null and b/lib/android/appcompat-v7-28.0.0.jar differ diff --git a/lib/android/dx.jar b/lib/android/dx.jar new file mode 100644 index 0000000..0e0aec0 Binary files /dev/null and b/lib/android/dx.jar differ diff --git a/lib/ant-contrib/ant-contrib-1.0b3.jar b/lib/ant-contrib/ant-contrib-1.0b3.jar new file mode 100644 index 0000000..0625376 Binary files /dev/null and b/lib/ant-contrib/ant-contrib-1.0b3.jar differ diff --git a/lib/appinventor/AndroidRuntime.jar b/lib/appinventor/AndroidRuntime.jar new file mode 100644 index 0000000..6b16e8d Binary files /dev/null and b/lib/appinventor/AndroidRuntime.jar differ diff --git a/lib/appinventor/AnnotationProcessors.jar b/lib/appinventor/AnnotationProcessors.jar new file mode 100644 index 0000000..06fb587 Binary files /dev/null and b/lib/appinventor/AnnotationProcessors.jar differ diff --git a/lib/deps/.placeholder b/lib/deps/.placeholder new file mode 100644 index 0000000..5b90c8b --- /dev/null +++ b/lib/deps/.placeholder @@ -0,0 +1 @@ +Any external dependencies should be placed in this directory. diff --git a/lib/kawa/kawa-1.11-changes.diff b/lib/kawa/kawa-1.11-changes.diff new file mode 100644 index 0000000..bd7afd0 --- /dev/null +++ b/lib/kawa/kawa-1.11-changes.diff @@ -0,0 +1,106 @@ +==== gnu/bytecode/ObjectType.java#1 - gnu/bytecode/ObjectType.java#2 ==== +45c45,49 +< return t.isExisting(); +--- +> // start Google +> return ! (t instanceof ObjectType) || ((ObjectType) t).isExisting(); +> // instead of +> // return t.isExisting(); +> // end Google +89a94,100 +> // start Google +> try +> { +> return Class.forName(cname, false, ObjectType.class.getClassLoader()); +> } +> catch (java.lang.ClassNotFoundException ex) +> { +90a102,105 +> } +> // instead of +> // return Class.forName(cname, false, getContextClassLoader()); +> // end Google +171a187 +> // if Google +172a189 +> // end Google +176c193,197 +< return getMethods(filter, searchSupers, result); +--- +> // if Google +> return Type.objectType.getMethods(filter, searchSupers, result, context); +> // instead of +> // return getMethods(filter, searchSupers, result); +> // end Google +==== gnu/expr/Compilation.java#1 - gnu/expr/Compilation.java#2 ==== +2444,2445c2444,2446 +< if (! mustCompile && ! ModuleExp.compilerAvailable) +< error('e', "this expression must be compiled, but compiler is unavailable"); +--- +> if (! mustCompile && ! ModuleExp.compilerAvailable) { +> error('w', "this expression claimed that it must be compiled, but compiler is unavailable"); +> } else { +2447a2449 +> } +==== gnu/expr/ModuleExp.java#1 - gnu/expr/ModuleExp.java#2 ==== +174a175,190 +> // start Google +> /** Flag to force no compilation */ +> public static boolean neverCompile = false; +> +> public static void mustNeverCompile() { +> alwaysCompile = false; +> neverCompile = true; +> compilerAvailable = false; +> } +> +> public static void mustAlwaysCompile() { +> alwaysCompile = true; +> neverCompile = false; +> } +> // end Google +> +204a221,234 +> +> // if Google -- NOTE: EVERYTHING FAILS IF THESE ENABLED +> // alwaysCompile = false; +> // neverCompile = true; +> // compilerAvailable = false; +> +> if (alwaysCompile && neverCompile) +> { +> throw new RuntimeException("alwaysCompile and neverCompile are both true!"); +> } +> if (neverCompile) +> comp.mustCompile = false; +> // end Google +> +212a243,246 +> // if Google +> // if (! alwaysCompile && ! comp.mustCompile) +> // { // optimization - don't generate unneeded Class. +> // instead of +214a249 +> // end Google +244c279 +< return null; +--- +> return false; +273c308 +< if (inst instanceof Class) +--- +> if (inst instanceof Class) { +275c310 +< +--- +> } +==== gnu/text/Path.java#1 - gnu/text/Path.java#2 ==== +13c13 +< implements javax.tools.FileObject +--- +> // implements javax.tools.FileObject +==== kawa/Version.java#1 - kawa/Version.java#2 ==== +5c5 +< return "1.11 (revision 6811M)"; +--- +> return "1.11"; diff --git a/lib/kawa/kawa-1.11-modified.jar b/lib/kawa/kawa-1.11-modified.jar new file mode 100644 index 0000000..cb91cc0 Binary files /dev/null and b/lib/kawa/kawa-1.11-modified.jar differ diff --git a/lib/proguard/LICENSE.md b/lib/proguard/LICENSE.md new file mode 100644 index 0000000..c4c141a --- /dev/null +++ b/lib/proguard/LICENSE.md @@ -0,0 +1,259 @@ +# GNU GENERAL PUBLIC LICENSE + +Version 2, June 1991 + +Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place - +Suite 330, Boston, MA 02111-1307, USA + +Everyone is permitted to copy and distribute verbatim copies of this license +document, but changing it is not allowed. + +## Preamble + +The licenses for most software are designed to take away your freedom to share +and change it. By contrast, the GNU General Public License is intended to +guarantee your freedom to share and change free software--to make sure the +software is free for all its users. This General Public License applies to +most of the Free Software Foundation's software and to any other program whose +authors commit to using it. (Some other Free Software Foundation software is +covered by the GNU Library General Public License instead.) You can apply it +to your programs, too. + +When we speak of free software, we are referring to freedom, not price. Our +General Public Licenses are designed to make sure that you have the freedom to +distribute copies of free software (and charge for this service if you wish), +that you receive source code or can get it if you want it, that you can change +the software or use pieces of it in new free programs; and that you know you +can do these things. + +To protect your rights, we need to make restrictions that forbid anyone to +deny you these rights or to ask you to surrender the rights. These +restrictions translate to certain responsibilities for you if you distribute +copies of the software, or if you modify it. + +For example, if you distribute copies of such a program, whether gratis or for +a fee, you must give the recipients all the rights that you have. You must +make sure that they, too, receive or can get the source code. And you must +show them these terms so they know their rights. + +We protect your rights with two steps: (1) copyright the software, and (2) +offer you this license which gives you legal permission to copy, distribute +and/or modify the software. + +Also, for each author's protection and ours, we want to make certain that +everyone understands that there is no warranty for this free software. If the +software is modified by someone else and passed on, we want its recipients to +know that what they have is not the original, so that any problems introduced +by others will not reflect on the original authors' reputations. + +Finally, any free program is threatened constantly by software patents. +We wish to avoid the danger that redistributors of a free program will +individually obtain patent licenses, in effect making the program +proprietary. To prevent this, we have made it clear that any patent must +be licensed for everyone's free use or not licensed at all. + +The precise terms and conditions for copying, distribution and modification +follow. + +## TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +**0.** This License applies to any program or other work which contains a +notice placed by the copyright holder saying it may be distributed under the +terms of this General Public License. The "Program", below, refers to any such +program or work, and a "work based on the Program" means either the Program or +any derivative work under copyright law: that is to say, a work containing the +Program or a portion of it, either verbatim or with modifications and/or +translated into another language. (Hereinafter, translation is included +without limitation in the term "modification".) Each licensee is addressed as +"you". + +Activities other than copying, distribution and modification are not covered +by this License; they are outside its scope. The act of running the Program is +not restricted, and the output from the Program is covered only if its +contents constitute a work based on the Program (independent of having been +made by running the Program). Whether that is true depends on what the Program +does. + +**1.** You may copy and distribute verbatim copies of the Program's source +code as you receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice and +disclaimer of warranty; keep intact all the notices that refer to this License +and to the absence of any warranty; and give any other recipients of the +Program a copy of this License along with the Program. + +You may charge a fee for the physical act of transferring a copy, and you may +at your option offer warranty protection in exchange for a fee. + +**2.** You may modify your copy or copies of the Program or any portion of it, +thus forming a work based on the Program, and copy and distribute such +modifications or work under the terms of Section 1 above, provided that you +also meet all of these conditions: + +- **a)** You must cause the modified files to carry prominent notices stating + that you changed the files and the date of any change. +- **b)** You must cause any work that you distribute or publish, that in whole + or in part contains or is derived from the Program or any part thereof, to + be licensed as a whole at no charge to all third parties under the terms of + this License. +- **c)** If the modified program normally reads commands interactively when + run, you must cause it, when started running for such interactive use in the + most ordinary way, to print or display an announcement including an + appropriate copyright notice and a notice that there is no warranty (or + else, saying that you provide a warranty) and that users may redistribute + the program under these conditions, and telling the user how to view a copy + of this License. (Exception: if the Program itself is interactive but does + not normally print such an announcement, your work based on the Program is + not required to print an announcement.) + +These requirements apply to the modified work as a whole. If identifiable +sections of that work are not derived from the Program, and can be reasonably +considered independent and separate works in themselves, then this License, +and its terms, do not apply to those sections when you distribute them as +separate works. But when you distribute the same sections as part of a whole +which is a work based on the Program, the distribution of the whole must be on +the terms of this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest your +rights to work written entirely by you; rather, the intent is to exercise the +right to control the distribution of derivative or collective works based on +the Program. + +In addition, mere aggregation of another work not based on the Program with +the Program (or with a work based on the Program) on a volume of a storage or +distribution medium does not bring the other work under the scope of this +License. + +**3.** You may copy and distribute the Program (or a work based on it, under +Section 2) in object code or executable form under the terms of Sections 1 and +2 above provided that you also do one of the following: + +- **a)** Accompany it with the complete corresponding machine-readable source + code, which must be distributed under the terms of Sections 1 and 2 above on + a medium customarily used for software interchange; or, +- **b)** Accompany it with a written offer, valid for at least three years, to + give any third party, for a charge no more than your cost of physically + performing source distribution, a complete machine-readable copy of the + corresponding source code, to be distributed under the terms of Sections 1 + and 2 above on a medium customarily used for software interchange; or, +- **c)** Accompany it with the information you received as to the offer to + distribute corresponding source code. (This alternative is allowed only for + noncommercial distribution and only if you received the program in object + code or executable form with such an offer, in accord with Subsection b + above.) + +The source code for a work means the preferred form of the work for making +modifications to it. For an executable work, complete source code means all +the source code for all modules it contains, plus any associated interface +definition files, plus the scripts used to control compilation and +installation of the executable. However, as a special exception, the source +code distributed need not include anything that is normally distributed (in +either source or binary form) with the major components (compiler, kernel, and +so on) of the operating system on which the executable runs, unless that +component itself accompanies the executable. + +If distribution of executable or object code is made by offering access to +copy from a designated place, then offering equivalent access to copy the +source code from the same place counts as distribution of the source code, +even though third parties are not compelled to copy the source along with the +object code. + +**4.** You may not copy, modify, sublicense, or distribute the Program except +as expressly provided under this License. Any attempt otherwise to copy, +modify, sublicense or distribute the Program is void, and will automatically +terminate your rights under this License. However, parties who have received +copies, or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + +**5.** You are not required to accept this License, since you have not signed +it. However, nothing else grants you permission to modify or distribute the +Program or its derivative works. These actions are prohibited by law if you do +not accept this License. Therefore, by modifying or distributing the Program +(or any work based on the Program), you indicate your acceptance of this +License to do so, and all its terms and conditions for copying, distributing +or modifying the Program or works based on it. + +**6.** Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the original +licensor to copy, distribute or modify the Program subject to these terms and +conditions. You may not impose any further restrictions on the recipients' +exercise of the rights granted herein. You are not responsible for enforcing +compliance by third parties to this License. + +**7.** If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or otherwise) +that contradict the conditions of this License, they do not excuse you from +the conditions of this License. If you cannot distribute so as to satisfy +simultaneously your obligations under this License and any other pertinent +obligations, then as a consequence you may not distribute the Program at all. +For example, if a patent license would not permit royalty-free redistribution +of the Program by all those who receive copies directly or indirectly through +you, then the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply and +the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any patents or +other property right claims or to contest validity of any such claims; this +section has the sole purpose of protecting the integrity of the free software +distribution system, which is implemented by public license practices. Many +people have made generous contributions to the wide range of software +distributed through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing to +distribute software through any other system and a licensee cannot impose that +choice. + +This section is intended to make thoroughly clear what is believed to be a +consequence of the rest of this License. + +**8.** If the distribution and/or use of the Program is restricted in certain +countries either by patents or by copyrighted interfaces, the original +copyright holder who places the Program under this License may add an explicit +geographical distribution limitation excluding those countries, so that +distribution is permitted only in or among countries not thus excluded. In +such case, this License incorporates the limitation as if written in the body +of this License. + +**9.** The Free Software Foundation may publish revised and/or new versions of +the General Public License from time to time. Such new versions will be +similar in spirit to the present version, but may differ in detail to address +new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any later +version", you have the option of following the terms and conditions either of +that version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of this License, +you may choose any version ever published by the Free Software Foundation. + +**10.** If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author to +ask for permission. For software which is copyrighted by the Free Software +Foundation, write to the Free Software Foundation; we sometimes make +exceptions for this. Our decision will be guided by the two goals of +preserving the free status of all derivatives of our free software and of +promoting the sharing and reuse of software generally. + +**NO WARRANTY** + +**11.** BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE +THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND +PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, +YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + +**12.** IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO +LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR +THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. diff --git a/lib/proguard/LICENSE_exception.md b/lib/proguard/LICENSE_exception.md new file mode 100644 index 0000000..d236ded --- /dev/null +++ b/lib/proguard/LICENSE_exception.md @@ -0,0 +1,36 @@ +# Special Exception to the GNU General Public License + +Copyright © 2002-2019 Guardsquare NV + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA + +In addition, as a special exception, Guardsquare NV gives permission to link +the code of this program with the following stand-alone applications: + +- Gradle, +- Apache Ant, +- Apache Maven, +- the Google Android SDK, +- the Intel TXE/DAL SDK, +- the Eclipse ProGuardDT GUI, +- the EclipseME JME IDE, +- the Oracle NetBeans Java IDE, +- the Oracle JME Wireless Toolkit, and +- the Simple Build Tool for Scala (and its scripts). + +and distribute linked combinations including the two. You must obey the GNU +General Public License in all respects for all of the code used other than +these programs. If you modify this file, you may extend this exception to your +version of the file, but you are not obligated to do so. If you do not wish to +do so, delete this exception statement from your version. diff --git a/lib/proguard/README.md b/lib/proguard/README.md new file mode 100644 index 0000000..157448a --- /dev/null +++ b/lib/proguard/README.md @@ -0,0 +1,64 @@ +ProGuard, Java bytecode optimizer and obfuscator +================================================ + +This distribution contains the following directories: + +- bin : simple wrapper scripts to run ProGuard, its GUI, and ReTrace +- lib : the main jars, compiled and ready to use with "java -jar ...." +- examples : some example configuration files and projects + +It also contains the source code and build scripts: + +- core : the ProGuard core +- retrace : the ReTrace tool +- gui : the ProGuard/ReTrace GUI +- gradle : the ProGuard Gradle plugin +- ant : the ProGuard Ant plugin +- wtk : the ProGuard WTK plugin +- annotations : the optional annotations to configure ProGuard +- buildscripts : various alternative build scripts + +The best place to start is the [on-line manual](https://www.guardsquare.com/proguard). + + +Example +------- + +If you want to give ProGuard a spin right away, try processing the ProGuard +jar itself: + + cd examples/standalone + ../../bin/proguard.sh @ proguard.pro + +The resulting proguard\_out.jar contains the same application, but it's a lot +smaller. + + +Android example +--------------- + +If you want to see this version of ProGuard integrated in an Android project, +you can look at the small Android HelloWorld project: + + cd examples/android + gradle assembleRelease + + +Downloads +--------- + +You can download ProGuard in various forms: + +- [Pre-built artifacts](https://bintray.com/guardsquare/proguard) at JCenter +- [Pre-built artifacts](https://search.maven.org/search?q=g:net.sf.proguard) at Maven Central +- [Traditional pre-built archives](https://sourceforge.net/projects/proguard/files/) at Sourceforge +- A [Mercurial repository of the source code](https://sourceforge.net/p/proguard/code/) at Sourceforge +- A [Git repository of the source code](https://github.com/Guardsquare/proguard) at Github +- The [complete ProGuard manual](https://www.guardsquare.com/proguard) at Guardsquare + + +Enjoy! + +https://www.guardsquare.com/proguard + +Copyright (c) 2002-2019 Guardsquare NV diff --git a/lib/proguard/proguard.cfg b/lib/proguard/proguard.cfg new file mode 100644 index 0000000..49a7e18 --- /dev/null +++ b/lib/proguard/proguard.cfg @@ -0,0 +1,13 @@ +-verbose +-dontnote ** +-optimizationpasses 3 +-allowaccessmodification +-dontskipnonpubliclibraryclasses +-mergeinterfacesaggressively +-overloadaggressively +-useuniqueclassmembernames +-repackageclasses '' + +-keep public class * { + public protected *; +} \ No newline at end of file diff --git a/lib/proguard/proguard.jar b/lib/proguard/proguard.jar new file mode 100644 index 0000000..6733d7a Binary files /dev/null and b/lib/proguard/proguard.jar differ diff --git a/src/com/yusufcihan/DynamicComponents/DynamicComponents.java b/src/com/yusufcihan/DynamicComponents/DynamicComponents.java index 0a648a3..8ba731d 100644 --- a/src/com/yusufcihan/DynamicComponents/DynamicComponents.java +++ b/src/com/yusufcihan/DynamicComponents/DynamicComponents.java @@ -4,24 +4,19 @@ import com.google.appinventor.components.runtime.*; import com.google.appinventor.components.common.*; import com.google.appinventor.components.runtime.util.YailList; + import com.google.appinventor.components.runtime.errors.YailRuntimeError; import java.util.Hashtable; -import java.util.Iterator; import java.util.ArrayList; import java.util.List; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.lang.reflect.Parameter; -import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Set; import java.lang.Boolean; -import android.view.View; -import android.view.ViewGroup; - @DesignerComponent(version = 3, description = "Dynamic Components extension to create any type of dynamic component in any arrangement.

- by Yusuf Cihan", category = ComponentCategory.EXTENSION, @@ -31,184 +26,261 @@ public class DynamicComponents extends AndroidNonvisibleComponent implements Component { // Variables - private Hashtable COMPONENTS = new Hashtable(); - private List blacklist = Arrays.asList("CopyHeight", "CopyWidth", "wait", "onClick", "Column", "Row", "setLastWidth", "setLastHeight"); + private Hashtable COMPONENTS = new Hashtable(); private String BASE_PACKAGE = "com.google.appinventor.components.runtime"; private String LAST_ID = ""; public DynamicComponents(ComponentContainer container) { super(container.$form()); } - + + /* private String BasePackage() { - return BASE_PACKAGE; + return BASE_PACKAGE; } private void BasePackage(String packageName) { - BASE_PACKAGE = packageName; + BASE_PACKAGE = packageName; } + */ + + // ------------------------ // MAIN METHODS // ------------------------ - @SimpleFunction(description = "Create a dynamic component that you want. It supports all components that added to App Inventor sources. Use 'in' parameter to specify the arrangement or canvas which new component will be placed in. Type the name of component in the 'componentName' section. (case sensitive) Use any component blocks to edit dynamic component's properties! ") - public void Create(AndroidViewComponent in, String componentName, String id) { - Object component = null; + /* + Creates a new dynamic component. It supports all component that added to your current AI2 builder. + In componentName, you can type the component's name like "Button", or you can pass a static component then it can create a new instance of it. + */ + @SimpleFunction(description = + "Creates a new dynamic component. It supports all component that added to your current AI2 builder.\n" + + "In componentName, you can type the component's name like 'Button',\n" + + "or you can pass a static component then it can create a new instance of it.") + public void Create(AndroidViewComponent in, Object componentName, String id) { + Component component = null; LAST_ID = id; + String error = null; // Check if id is used by another created dynamic component. if (!COMPONENTS.containsKey(id)) { - try - { - // Return the component class by looking the its name. - Class clasz = Class.forName(BASE_PACKAGE + "." + componentName); - // Create constructor object for creating a new instance. - Constructor constructor = clasz.getConstructor(new Class[] { ComponentContainer.class }); - // Create a new instance of specified component. - component = constructor.newInstance((ComponentContainer)in); - } - catch (Exception e) - { - // Throw a runtime error when something goes wrong. - throw new YailRuntimeError(e.getMessage(),"Error"); - } - COMPONENTS.put(id, component); + try + { + // If input is a component name then create a instance of it. + if (componentName instanceof String) + { + // Return the component class by looking the its name. + Class clasz = Class.forName(BASE_PACKAGE + "." + componentName.toString().replace(" ", "")); + // Create constructor object for creating a new instance. + Constructor constructor = clasz.getConstructor(new Class[] { ComponentContainer.class }); + // Create a new instance of specified component. + component = (Component)constructor.newInstance((ComponentContainer)in); + } + else + { + String packageName = componentName.getClass().getPackage().getName(); + if (packageName.equals(BASE_PACKAGE)) + { + Class clasz = Class.forName(componentName.getClass().getName()); + Constructor constructor = clasz.getConstructor(new Class[] { ComponentContainer.class }); + component = (Component)constructor.newInstance((ComponentContainer)in); + } + else + { + error = "Input is not a string or a valid component type."; + } + + } + + } + catch (Exception e) + { + error = e.getMessage(); + } } else { - // Throw a runtime error when ID is already used for another component. - throw new YailRuntimeError("This ID is already used, please pick another.","Duplicate ID"); - } - - } - - @SimpleFunction(description = "Removes the component with specified ID from screen/layout and the component list. So you will able to use its ID again as it will be deleted.") + error = "This ID is already used for another component, please pick another. ID needs to be unique for all components!"; + } + + if ((id.trim().length() == 0) || (id == null)) + { + error = "ID is blank. Please enter a valid ID."; + } + + if (error != null) + { + throw new YailRuntimeError(error, "DynamicComponents-AI2 Error"); + } + else { + COMPONENTS.put(id, component); + } + } + + /* + Changes ID of one of created components to a new one. The old ID must be exist and new ID mustn't exist. + */ + @SimpleFunction(description = "Changes ID of one of created components to a new one. The old ID must be exist and new ID mustn't exist.") + public void ChangeId(String id, String newId) { + if (COMPONENTS.containsKey(id) && !COMPONENTS.containsKey(newId)) + { + Component component = COMPONENTS.remove(id); + COMPONENTS.put(newId, component); + } + else + { + throw new YailRuntimeError("Old ID must exist and new ID mustn't exist.","Error"); + } + } + + /* + Removes the component with specified ID from screen/layout and the component list. So you will able to use its ID again as it will be deleted. + */ + @SimpleFunction(description = "Removes the component with specified ID from screen/layout and the component list. So you will able to use its ID again as it will be deleted.") public void Remove(String id) { - Method m = null; - Object cmp = null; // Don't do anything if id is not in the components list. - if (COMPONENTS.containsKey(id) == false) - return; - - // Get the component. - cmp = COMPONENTS.get(id); - // Remove its id from components list. - COMPONENTS.remove(id); - - try - { - // Hide the component if possible. - ((AndroidViewComponent)cmp).Visible(false); + if (COMPONENTS.containsKey(id)) + { + // Get the component. + Object cmp = COMPONENTS.get(id); + + try { + Method m = cmp.getClass().getMethod("Visible", boolean.class); + m.invoke(cmp, false); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + // Remove its id from components list. + COMPONENTS.remove(id); } - catch (Exception eh) { } } - - @SimpleFunction(description = "Returns last used ID.") + + /* + Returns last used ID by Create block. + */ + @SimpleFunction(description = "Returns last used ID by Create block.") public String LastUsedID() { return LAST_ID; } - - @SimpleFunction(description = "Returns the component's itself for setting properties. Component needs to be created with Create block. Type an ID which you typed in Create block to return the component.") - public Object GetComponent(String id) { + + /* + Returns all used IDs in the created components list. + */ + @SimpleFunction(description = "Returns all used IDs in the created components list.") + public YailList UsedIDs() { + Set keys = COMPONENTS.keySet(); + return YailList.makeList(keys); + } + + /* + Returns the component's itself for setting properties. ID must be a valid ID which is added with Create block. + */ + @SimpleFunction(description = "Returns the component's itself for setting properties. ID must be a valid ID which is added with Create block.") + public Component GetComponent(String id) { return COMPONENTS.get(id); } - - @SimpleFunction(description = "Returns created components' IDs as a list.") - public Object GetCreatedComponents() { - Set keys = COMPONENTS.keySet(); - return keys; + + /* + Returns the ID of component. Component needs to be created by Create block. Otherwise it will return blank string. + */ + @SimpleFunction(description = "Returns the ID of component. Component needs to be created by Create block. Otherwise it will return blank string.") + public String GetId(Component component) { + return getKeyFromValue(COMPONENTS, component); } - - @SimpleFunction(description = "Returns the component type name.") - public String GetName(Object component) { - return component.getClass().getName(); + + /* + Returns the component's name. + */ + @SimpleFunction(description = "Returns the component's name.") + public String GetName(Component component) { + return component.getClass().getName().replace(BASE_PACKAGE + ".", ""); } - - @SimpleFunction(description = "Removes all created dynamic components. Same as Remove block, but for all created components.") - public void RemoveAll() { + + /* + Removes all created dynamic components. Same as Remove block, but for all created components. + */ + @SimpleFunction(description = "Removes all created dynamic components. Same as Remove block, but for all created components.") + private void RemoveAll() { Set keys = COMPONENTS.keySet(); - for(String key: keys){ + for (String key : keys) { Remove(key); } } - @SimpleFunction(description = "Get all available properties of a component which can be set from Designer as list along with types. Can be used to learn the properties of any component which is not static.") - public YailList GetDesignerProperties(Object component) { - // A list which includes designer properties. - ArrayList names = new ArrayList(); - // Get the component's class and return all methods from it. - Method[] methods = component.getClass().getMethods(); - for (Method mtd : methods) - { - // Read for @DesignerProperty annotations. - // So we can learn which method is used as property setter/getter. - if ((mtd.getDeclaredAnnotations().length == 2) && (mtd.isAnnotationPresent(DesignerProperty.class))) - { - // Get the DesignerProperty annotation. - DesignerProperty n = mtd.getAnnotation(DesignerProperty.class); - // Add editorType value and method name to the list. - names.add(YailList.makeList(new String[] { - mtd.getName(), - n.editorType() - })); - } - } - // Return the list. - return YailList.makeList(names); - } - - @SimpleFunction(description = "Set a property of a component by typing its name.") - public void SetProperty(Object component, String propertyName, Object propertyValue) { - // Read methods of the component. - Method[] methods = component.getClass().getMethods(); + /* + Set a property of a component by typing its name. + */ + @SimpleFunction(description = "Set a property of a component by typing its property name.") + public void SetProperty(Component component, String name, Object value) { // The method will be invoked. - Method method = null; - // Class for casting purpose. - Class caster = null; + Method m = null; try { - for (Method mtd : methods) - { - // Check for one parametered (setter) method. - if((mtd.getName() == propertyName) && (mtd.getParameterCount() == 1)) - { - // Save it for later. - caster = mtd.getParameterTypes()[0]; - method = mtd; - break; - } - } - // Invoke the saved method. - method.invoke(component, propertyValue); + m = FindMethod(component.getClass().getMethods(), name, 1); + // Method m = component.getClass().getMethod(name, value.getClass()); + if (m == null) + throw new YailRuntimeError("Property can't found with that name.", "Error"); + + String outputName = m.getParameterTypes()[0].getName().toString().trim(); + String inputName = value.getClass().getName().toString().trim(); + String v = ""; + + // Parse the value and save it in a variable. + if (inputName.equals("gnu.math.IntNum")) + { + v = Integer.toString(((gnu.math.IntNum)value).intValue()); + } + else if (inputName.equals("gnu.math.DFloNum")) + { + v = Double.toString(((gnu.math.DFloNum)value).doubleValue()); + } + else { + v = value.toString(); + } + + // Check for requested parameter type. + if (outputName.equals("int")) { + m.invoke(component, Integer.parseInt(v)); + } + else if (outputName.equals("double")) { + m.invoke(component, Double.parseDouble(v)); + } + else if (outputName.equals("float")) { + m.invoke(component, Float.parseFloat(v)); + } + else { + m.invoke(component, Class.forName(value.getClass().getName()).cast(value)); + } } - catch (Exception eh) + catch (InvocationTargetException | IllegalAccessException | ClassNotFoundException eh) { - // Throw an error when something goes wrong. - throw new YailRuntimeError(eh.getMessage().toString(),"Error"); + throw new YailRuntimeError(eh.getMessage().toString(),"Error"); + } + catch (IllegalArgumentException eh) { + throw new YailRuntimeError("Looks like parameters are invalid. If you think everything is correct, please report.", "Error"); + } + catch (Exception e) + { + throw new YailRuntimeError(e.getClass().getName() + ": " + e.getMessage().toString(), "Error"); } } - + + /* + Get property value of a component. + */ @SimpleFunction(description = "Get property value of a component.") - public Object GetProperty(Object component, String propertyName) { - // Read methods of the component. - Method[] methods = component.getClass().getMethods(); + public Object GetProperty(Component component, String name) { // The method will be invoked. - Method method = null; + Method m = null; try { - for (Method mtd : methods) - { - // Check for zero parametered (getter) method. - if((mtd.getName() == propertyName) && (mtd.getParameterCount() == 0)) - { - // Save it for later. - method = mtd; - break; - } - } - // Invoke the saved method and return its return value. - return method.invoke(component); + m = FindMethod(component.getClass().getMethods(), name, 0); + // Invoke the saved method and return its return value. + return m.invoke(component); } catch (Exception eh) { @@ -217,10 +289,41 @@ public Object GetProperty(Object component, String propertyName) { } } - @SimpleFunction(description = "Returns the ID of component. Component needs to be created by Create block. Otherwise it will return -1.") - public String GetId(Object component) { - return getKeyFromValue(COMPONENTS, component); + @SimpleFunction(description = "Get all available properties of a component which can be set from Designer as list along with types. Can be used to learn the properties of any component which is not static.") + public YailList GetDesignerProperties(Component component) { + // A list which includes designer properties. + List properties = new ArrayList(); + // Get the component's class and return all methods from it. + Method[] methods = component.getClass().getMethods(); + for (Method mtd : methods) + { + // Read for @DesignerProperty annotations. + // So we can learn which method is used as property setter/getter. + if ((mtd.getDeclaredAnnotations().length == 2) && (mtd.isAnnotationPresent(DesignerProperty.class))) + { + // Get the DesignerProperty annotation. + DesignerProperty n = mtd.getAnnotation(DesignerProperty.class); + // Add editorType value and method name to the list. + properties.add(mtd.getName() + "---" + n.editorType()); + } + } + // Return the list. + return YailList.makeList(properties); + } + + @SimpleFunction(description = "Get all available methods from a component.") + private YailList GetMethods(Component component) { + // A list which includes designer properties. + List names = new ArrayList(); + for (Method mtd : component.getClass().getMethods()) + { + names.add(mtd.getName()); + } + // Return the list. + return YailList.makeList(names); } + + // ------------------------ // PRIVATE METHODS @@ -228,12 +331,27 @@ public String GetId(Object component) { // Getting key from value, found on: // http://www.java2s.com/Code/Java/Collections-Data-Structure/GetakeyfromvaluewithanHashMap.htm - public String getKeyFromValue(Hashtable hm, Object value) { - for (Object o : hm.keySet()) { - if (hm.get(o).equals(value)) { - return (String)o; - } + public String getKeyFromValue(Hashtable hm, Object value) { + for (String o : hm.keySet()) { + if (hm.get(o).equals(value)) { + return (String)o; + } } - return ""; + return ""; } + + private Method FindMethod(Method[] methods, String name, Integer paramCount) { + Method m = null; + for (Method mtd : methods) + { + // Check for one parametered (setter) method. + if((mtd.getName() == name.trim()) && (mtd.getParameterCount() == paramCount)) + { + m = mtd; + break; + } + } + return m; + } + }