diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 95d6e64..0000000 --- a/.gitignore +++ /dev/null @@ -1,30 +0,0 @@ -tmp/ -*.tmp -*.bak -*.log -*~* -*.iml -out/ -.idea/ -*.ipr -*.iws -.gradle -app/build/ -build/ -gradle-app.setting -!gradle-wrapper.jar -*.class -.mtj.tmp/ -*.jar -*.war -*.ear -hs_err_pid* -*.apk -*.ap_ -*.dex -bin/ -gen/ -local.properties -proguard/ -.navigation/ -gen-external-apklibs diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 177a4d0..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,99 +0,0 @@ -# Changelog EnigmAndroid - -## v1.0.2-01.01.2019 -### Changes -* Added support for fastlane metadata -* Remove What's new dialog - -## v1.0.1-12.01.2018 -### Changes -* Added language: Brazilian-Portuguese - -## v1.0.0-05.05.2017 -### Changes -* Added Enigma KD -* Added "protocol version" to QR-Code-shared configuration strings. - This breaks backwards compatibility to older versions, but I added it to enable backwards compatibility in upcoming releases. -* Configurations can now be shared to EnigmAndroid as text -* Moved preference management to SettingsActivity -* Added dialog to choose whether to share configuration via text or via QR-code -* Same for receiving -* Added TextBox to configuration-share-dialog that lets the user see and copy the - configuration string. -* Added Whats-new-Dialog -* New Icon! -* Added Script to automatically generate icons -* Reformatted code -### TODO -* Add tips on long clicks at parts -* Move KD right below K -* Add intent filters to recognize and automatically handle shared/copied configuration Strings .These are Strings starting with "EnigmAndroid/" -* Write tests to ensure correct functionality (Pull Requests welcome) -* Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon) - -## v0.1.9-09.10.2015 -### Changes -* Added option to share/receive configurations via QR-Code (ZXing Barcode Scanner) -* Prevent user from setting incomplete reflector wiring -* Add option to generate configuration from passphrase -* Reworked Enigma definition (available Rotors/Reflectors/ Entrywheels -* Completely verified correct functionality of Enigma T -* Added number spelling in spanish, italian -* Added backwards compatibility to API level 10 (Gingerbread 2.3.3) - -## v0.1.8-27.09.2015 -### Changes -* Added Enigma G31 -* Added Enigma G312 -* Added Enigma G260 -* Replaced Enigma K with Enigma K, K (Swiss) and K (Swiss, Airforce) -* Added Enigma R -* Changed identifiers of enigma models -* Changed landscape layout of enigma model d -* Updated the about-dialog text. -* Shortened EnigmaStateBundle -* Added different colors to the plugboard-/pluggable reflector dialog. This helps to differentiate connections. -* Reworked InputPreparer using decorator pattern and added options to customize input preparation -* Reworked Reflector-/Rotor creation/management -* Added Button to set the Enigma into a random configuration - -## v0.1.7-15.09.2015 -### Changes -* Added Enigma K -* Added Enigma T -* Created Plugboard-/pluggable Reflector-Setting-Dialog - -## v0.1.6-10.09.2015 -### Changes -* Fixed about dialog (outdated manual) -* Updated CHANGELOG (oops) -* Added Enigma D -* Added rotor names to ringSettingsDialog -* Reworked major parts of the app once again :) -* Added option to break messages up into blocks - -## v0.1.5-27.08.2015 -### Changes -* Added french number spelling -* Added Enigma Models M3, M4 -* Added option to select different enigma models into options menu -* Added developer class for simple rotor creation (not a feature in the app) -* Fixed broken ring settings -* Fixed false reset of ring settings when switching from/to landscape mode - -## v0.1.4-15.08.2015 -### Changes -* Reworked the core implementation to follow some principals of Software Engineering -* Fixed some layout issues -* Fixed anomaly for step by step inputs -* Added send/receive text functionality -* added missing licenses to class files -* Added proper documentation -* Extended input interpretation (number spelling in different languages, any unknown special character now becomes 'X' - -## v0.1.3-14.03.2015 -### Changes -* Added About Dialog with ChangeLog-Button -* Moved Version Info into About Dialog -* Updated License Files -* Fixed Landscape Layout diff --git a/CHANGELOG.txt b/CHANGELOG.txt new file mode 100755 index 0000000..e8a9896 --- /dev/null +++ b/CHANGELOG.txt @@ -0,0 +1,45 @@ +CHANGELOG ENIGMANDROID +v0.1.7-15.09.2015< +*Added Enigma K +*Added Enigma T +*Created Plugboard-/pluggable Reflector-Setting-Dialog +*TODO: Add Enigma Z (Probably wont happen due to lack of information :/) +*TODO: Add Enigma G +*TODO: Add Enigma R +*TODO: Rework Rotor-/Reflector creation +*TODO: Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon) +*TODO: Rework InputPreparer using decorator pattern to allow user customization +* + +v0.1.6-10.09.2015< +*Fixed about dialog (outdated manual) +*Updated CHANGELOG (oops) +*Added Enigma D +*Added rotor names to ringSettingsDialog +*Reworked major parts of the app once again :) +*Added option to break messages up into blocks + +v0.1.5-27.08.2015< +*Added french number spelling +*Added Enigma Models M3, M4 +*Added option to select different enigma models into options menu +*Added developer class for simple rotor creation (not a feature in the app) +*Fixed broken ring settings +*Fixed false reset of ring settings when switching from/to landscape mode + + +v0.1.4-15.08.2015< +*Reworked the core implementation to follow some principals of Software Engineering +*Fixed some layout issues +*Fixed anomaly for step by step inputs +*Added send/receive text functionality +*added missing licenses to class files +*Added proper documentation +*Extended input interpretation (number spelling in different languages, any unknown special +character now becomes 'X' + +v0.1.3-14.03.2015< +*Added About Dialog with ChangeLog-Button +*Moved Version Info into About Dialog +*Updated License Files +*Fixed Landscape Layout \ No newline at end of file diff --git a/EnigmAndroid.iml b/EnigmAndroid.iml new file mode 100755 index 0000000..fb2763b --- /dev/null +++ b/EnigmAndroid.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/License.md b/License.md old mode 100644 new mode 100755 index 97abe6d..77db3e0 --- a/License.md +++ b/License.md @@ -1,676 +1,280 @@ +GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to + 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 Lesser 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 -them 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. +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 prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. + 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 pass on to the recipients the same -freedoms that you received. 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. +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. - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. + 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. - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. + 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. - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. + 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 - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey 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; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of + GNU GENERAL PUBLIC LICENSE + 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. - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or + 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 convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. +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. - 13. Use with the GNU Affero General Public License. +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. - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. +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. - 14. Revised Versions of this License. +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will + 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 that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. +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. - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. + 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. - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. + NO WARRANTY - 15. Disclaimer of 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. - 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. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -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. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - {one line to give the program's name and a brief idea of what it does.} - Copyright (C) {year} {name of author} - - 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 3 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, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - {project} Copyright (C) {year} {fullname} - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. + 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. + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 96af7cf..4b380c1 --- a/README.md +++ b/README.md @@ -1,24 +1,2 @@ # EnigmAndroid - - -[Get it on F-Droid](https://f-droid.org/app/de.vanitasvitae.enigmandroid) - -Android implementation of the famous Enigma machine. - -Check out the website at [Jabberhead.tk](https://blog.jabberhead.tk/EnigmAndroid/). - -## Screenshots - -
- - -
- -
- - -
- -If you want to support me with a donation, feel free to do so :) - -btc: 1MW2DobbNFfcjxTSYzJhtkmiNS7efwaWFX +Android implementation of the famous Enigma machine diff --git a/app/.gitignore b/app/.gitignore deleted file mode 100644 index fa69b30..0000000 --- a/app/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/build -/src/main/java/de/vanitasvitae/enigmandroid/enigma/util/ -/src/androidTest/ -/src/main/res/render-icon.sh diff --git a/app/app.iml b/app/app.iml new file mode 100644 index 0000000..e002617 --- /dev/null +++ b/app/app.iml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle old mode 100644 new mode 100755 index c28fc80..2e85ff0 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,28 +1,23 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "25.0.3" + compileSdkVersion 23 + buildToolsVersion "22.0.1" defaultConfig { applicationId "de.vanitasvitae.enigmandroid" - minSdkVersion 11 - targetSdkVersion 25 - versionCode 18 - versionName "1.0.2-01.01.2019" + minSdkVersion 16 + targetSdkVersion 23 + versionCode 13 + versionName "0.1.7-15.09.2015-beta" } - buildTypes { release { } } - - lintOptions { - disable 'MissingTranslation' - } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:support-v4:25.3.1' + compile 'com.android.support:support-v4:23.0.0' } diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro old mode 100644 new mode 100755 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml old mode 100644 new mode 100755 index be4ee4b..568284b --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,10 +4,9 @@ + android:theme="@style/AppTheme" > @@ -16,7 +15,6 @@ - diff --git a/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java b/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java deleted file mode 100644 index 2a59e5c..0000000 --- a/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java +++ /dev/null @@ -1,506 +0,0 @@ -/* - * Copyright 2009 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.integration.android; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Fragment; -import android.content.ActivityNotFoundException; -import android.content.DialogInterface; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.net.Uri; -import android.os.Bundle; -import android.util.Log; - -/** - *

A utility class which helps ease integration with Barcode Scanner via {@link Intent}s. This is a simple - * way to invoke barcode scanning and receive the result, without any need to integrate, modify, or learn the - * project's source code.

- * - *

Initiating a barcode scan

- * - *

To integrate, create an instance of {@code IntentIntegrator} and call {@link #initiateScan()} and wait - * for the result in your app.

- * - *

It does require that the Barcode Scanner (or work-alike) application is installed. The - * {@link #initiateScan()} method will prompt the user to download the application, if needed.

- * - *

There are a few steps to using this integration. First, your {@link Activity} must implement - * the method {@link Activity#onActivityResult(int, int, Intent)} and include a line of code like this:

- * - *
{@code
- * public void onActivityResult(int requestCode, int resultCode, Intent intent) {
- *   IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
- *   if (scanResult != null) {
- *     // handle scan result
- *   }
- *   // else continue with any other code you need in the method
- *   ...
- * }
- * }
- * - *

This is where you will handle a scan result.

- * - *

Second, just call this in response to a user action somewhere to begin the scan process:

- * - *
{@code
- * IntentIntegrator integrator = new IntentIntegrator(yourActivity);
- * integrator.initiateScan();
- * }
- * - *

Note that {@link #initiateScan()} returns an {@link AlertDialog} which is non-null if the - * user was prompted to download the application. This lets the calling app potentially manage the dialog. - * In particular, ideally, the app dismisses the dialog if it's still active in its {@link Activity#onPause()} - * method.

- * - *

You can use {@link #setTitle(String)} to customize the title of this download prompt dialog (or, use - * {@link #setTitleByID(int)} to set the title by string resource ID.) Likewise, the prompt message, and - * yes/no button labels can be changed.

- * - *

Finally, you can use {@link #addExtra(String, Object)} to add more parameters to the Intent used - * to invoke the scanner. This can be used to set additional options not directly exposed by this - * simplified API.

- * - *

By default, this will only allow applications that are known to respond to this intent correctly - * do so. The apps that are allowed to response can be set with {@link #setTargetApplications(List)}. - * For example, set to {@link #TARGET_BARCODE_SCANNER_ONLY} to only target the Barcode Scanner app itself.

- * - *

Sharing text via barcode

- * - *

To share text, encoded as a QR Code on-screen, similarly, see {@link #shareText(CharSequence)}.

- * - *

Some code, particularly download integration, was contributed from the Anobiit application.

- * - *

Enabling experimental barcode formats

- * - *

Some formats are not enabled by default even when scanning with {@link #ALL_CODE_TYPES}, such as - * PDF417. Use {@link #initiateScan(java.util.Collection)} with - * a collection containing the names of formats to scan for explicitly, like "PDF_417", to use such - * formats.

- * - * @author Sean Owen - * @author Fred Lin - * @author Isaac Potoczny-Jones - * @author Brad Drehmer - * @author gcstang - */ -public class IntentIntegrator { - - public static final int REQUEST_CODE = 0x0000c0de; // Only use bottom 16 bits - private static final String TAG = IntentIntegrator.class.getSimpleName(); - - public static final String DEFAULT_TITLE = "Install Barcode Scanner?"; - public static final String DEFAULT_MESSAGE = - "This application requires Barcode Scanner. Would you like to install it?"; - public static final String DEFAULT_YES = "Yes"; - public static final String DEFAULT_NO = "No"; - - private static final String BS_PACKAGE = "com.google.zxing.client.android"; - private static final String BSPLUS_PACKAGE = "com.srowen.bs.android"; - - // supported barcode formats - public static final Collection PRODUCT_CODE_TYPES = list("UPC_A", "UPC_E", "EAN_8", "EAN_13", "RSS_14"); - public static final Collection ONE_D_CODE_TYPES = - list("UPC_A", "UPC_E", "EAN_8", "EAN_13", "CODE_39", "CODE_93", "CODE_128", - "ITF", "RSS_14", "RSS_EXPANDED"); - public static final Collection QR_CODE_TYPES = Collections.singleton("QR_CODE"); - public static final Collection DATA_MATRIX_TYPES = Collections.singleton("DATA_MATRIX"); - - public static final Collection ALL_CODE_TYPES = null; - - public static final List TARGET_BARCODE_SCANNER_ONLY = Collections.singletonList(BS_PACKAGE); - public static final List TARGET_ALL_KNOWN = list( - BSPLUS_PACKAGE, // Barcode Scanner+ - BSPLUS_PACKAGE + ".simple", // Barcode Scanner+ Simple - BS_PACKAGE // Barcode Scanner - // What else supports this intent? - ); - - private final Activity activity; - private final Fragment fragment; - - private String title; - private String message; - private String buttonYes; - private String buttonNo; - private List targetApplications; - private final Map moreExtras = new HashMap<>(3); - - /** - * @param activity {@link Activity} invoking the integration - */ - public IntentIntegrator(Activity activity) { - this.activity = activity; - this.fragment = null; - initializeConfiguration(); - } - - /** - * @param fragment {@link Fragment} invoking the integration. - * {@link #startActivityForResult(Intent, int)} will be called on the {@link Fragment} instead - * of an {@link Activity} - */ - public IntentIntegrator(Fragment fragment) { - this.activity = fragment.getActivity(); - this.fragment = fragment; - initializeConfiguration(); - } - - private void initializeConfiguration() { - title = DEFAULT_TITLE; - message = DEFAULT_MESSAGE; - buttonYes = DEFAULT_YES; - buttonNo = DEFAULT_NO; - targetApplications = TARGET_ALL_KNOWN; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public void setTitleByID(int titleID) { - title = activity.getString(titleID); - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public void setMessageByID(int messageID) { - message = activity.getString(messageID); - } - - public String getButtonYes() { - return buttonYes; - } - - public void setButtonYes(String buttonYes) { - this.buttonYes = buttonYes; - } - - public void setButtonYesByID(int buttonYesID) { - buttonYes = activity.getString(buttonYesID); - } - - public String getButtonNo() { - return buttonNo; - } - - public void setButtonNo(String buttonNo) { - this.buttonNo = buttonNo; - } - - public void setButtonNoByID(int buttonNoID) { - buttonNo = activity.getString(buttonNoID); - } - - public Collection getTargetApplications() { - return targetApplications; - } - - public final void setTargetApplications(List targetApplications) { - if (targetApplications.isEmpty()) { - throw new IllegalArgumentException("No target applications"); - } - this.targetApplications = targetApplications; - } - - public void setSingleTargetApplication(String targetApplication) { - this.targetApplications = Collections.singletonList(targetApplication); - } - - public Map getMoreExtras() { - return moreExtras; - } - - public final void addExtra(String key, Object value) { - moreExtras.put(key, value); - } - - /** - * Initiates a scan for all known barcode types with the default camera. - * - * @return the {@link AlertDialog} that was shown to the user prompting them to download the app - * if a prompt was needed, or null otherwise. - */ - public final AlertDialog initiateScan() { - return initiateScan(ALL_CODE_TYPES, -1); - } - - /** - * Initiates a scan for all known barcode types with the specified camera. - * - * @param cameraId camera ID of the camera to use. A negative value means "no preference". - * @return the {@link AlertDialog} that was shown to the user prompting them to download the app - * if a prompt was needed, or null otherwise. - */ - public final AlertDialog initiateScan(int cameraId) { - return initiateScan(ALL_CODE_TYPES, cameraId); - } - - /** - * Initiates a scan, using the default camera, only for a certain set of barcode types, given as strings corresponding - * to their names in ZXing's {@code BarcodeFormat} class like "UPC_A". You can supply constants - * like {@link #PRODUCT_CODE_TYPES} for example. - * - * @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for - * @return the {@link AlertDialog} that was shown to the user prompting them to download the app - * if a prompt was needed, or null otherwise. - */ - public final AlertDialog initiateScan(Collection desiredBarcodeFormats) { - return initiateScan(desiredBarcodeFormats, -1); - } - - /** - * Initiates a scan, using the specified camera, only for a certain set of barcode types, given as strings corresponding - * to their names in ZXing's {@code BarcodeFormat} class like "UPC_A". You can supply constants - * like {@link #PRODUCT_CODE_TYPES} for example. - * - * @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for - * @param cameraId camera ID of the camera to use. A negative value means "no preference". - * @return the {@link AlertDialog} that was shown to the user prompting them to download the app - * if a prompt was needed, or null otherwise - */ - public final AlertDialog initiateScan(Collection desiredBarcodeFormats, int cameraId) { - Intent intentScan = new Intent(BS_PACKAGE + ".SCAN"); - intentScan.addCategory(Intent.CATEGORY_DEFAULT); - - // check which types of codes to scan for - if (desiredBarcodeFormats != null) { - // set the desired barcode types - StringBuilder joinedByComma = new StringBuilder(); - for (String format : desiredBarcodeFormats) { - if (joinedByComma.length() > 0) { - joinedByComma.append(','); - } - joinedByComma.append(format); - } - intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString()); - } - - // check requested camera ID - if (cameraId >= 0) { - intentScan.putExtra("SCAN_CAMERA_ID", cameraId); - } - - String targetAppPackage = findTargetAppPackage(intentScan); - if (targetAppPackage == null) { - return showDownloadDialog(); - } - intentScan.setPackage(targetAppPackage); - intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); - attachMoreExtras(intentScan); - startActivityForResult(intentScan, REQUEST_CODE); - return null; - } - - /** - * Start an activity. This method is defined to allow different methods of activity starting for - * newer versions of Android and for compatibility library. - * - * @param intent Intent to start. - * @param code Request code for the activity - * @see android.app.Activity#startActivityForResult(Intent, int) - * @see android.app.Fragment#startActivityForResult(Intent, int) - */ - protected void startActivityForResult(Intent intent, int code) { - if (fragment == null) { - activity.startActivityForResult(intent, code); - } else { - fragment.startActivityForResult(intent, code); - } - } - - private String findTargetAppPackage(Intent intent) { - PackageManager pm = activity.getPackageManager(); - List availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); - if (availableApps != null) { - for (String targetApp : targetApplications) { - if (contains(availableApps, targetApp)) { - return targetApp; - } - } - } - return null; - } - - private static boolean contains(Iterable availableApps, String targetApp) { - for (ResolveInfo availableApp : availableApps) { - String packageName = availableApp.activityInfo.packageName; - if (targetApp.equals(packageName)) { - return true; - } - } - return false; - } - - private AlertDialog showDownloadDialog() { - AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity); - downloadDialog.setTitle(title); - downloadDialog.setMessage(message); - downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - String packageName; - if (targetApplications.contains(BS_PACKAGE)) { - // Prefer to suggest download of BS if it's anywhere in the list - packageName = BS_PACKAGE; - } else { - // Otherwise, first option: - packageName = targetApplications.get(0); - } - Uri uri = Uri.parse("market://details?id=" + packageName); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - try { - if (fragment == null) { - activity.startActivity(intent); - } else { - fragment.startActivity(intent); - } - } catch (ActivityNotFoundException anfe) { - // Hmm, market is not installed - Log.w(TAG, "Google Play is not installed; cannot install " + packageName); - } - } - }); - downloadDialog.setNegativeButton(buttonNo, null); - downloadDialog.setCancelable(true); - return downloadDialog.show(); - } - - - /** - *

Call this from your {@link Activity}'s - * {@link Activity#onActivityResult(int, int, Intent)} method.

- * - * @param requestCode request code from {@code onActivityResult()} - * @param resultCode result code from {@code onActivityResult()} - * @param intent {@link Intent} from {@code onActivityResult()} - * @return null if the event handled here was not related to this class, or - * else an {@link IntentResult} containing the result of the scan. If the user cancelled scanning, - * the fields will be null. - */ - public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) { - if (requestCode == REQUEST_CODE) { - if (resultCode == Activity.RESULT_OK) { - String contents = intent.getStringExtra("SCAN_RESULT"); - String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT"); - byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES"); - int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE); - Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation; - String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL"); - return new IntentResult(contents, - formatName, - rawBytes, - orientation, - errorCorrectionLevel); - } - return new IntentResult(); - } - return null; - } - - - /** - * Defaults to name "TEXT_TYPE". - * - * @param text the text string to encode as a barcode - * @return the {@link AlertDialog} that was shown to the user prompting them to download the app - * if a prompt was needed, or null otherwise - * @see #shareText(CharSequence, CharSequence) - */ - public final AlertDialog shareText(CharSequence text) { - return shareText(text, "TEXT_TYPE"); - } - - /** - * Shares the given text by encoding it as a barcode, such that another user can - * scan the text off the screen of the device. - * - * @param text the text string to encode as a barcode - * @param type name of data to encode. See {@code com.google.zxing.client.android.Contents.Type} constants. - * @return the {@link AlertDialog} that was shown to the user prompting them to download the app - * if a prompt was needed, or null otherwise - */ - public final AlertDialog shareText(CharSequence text, CharSequence type) { - Intent intent = new Intent(); - intent.addCategory(Intent.CATEGORY_DEFAULT); - intent.setAction(BS_PACKAGE + ".ENCODE"); - intent.putExtra("ENCODE_TYPE", type); - intent.putExtra("ENCODE_DATA", text); - String targetAppPackage = findTargetAppPackage(intent); - if (targetAppPackage == null) { - return showDownloadDialog(); - } - intent.setPackage(targetAppPackage); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); - attachMoreExtras(intent); - if (fragment == null) { - activity.startActivity(intent); - } else { - fragment.startActivity(intent); - } - return null; - } - - private static List list(String... values) { - return Collections.unmodifiableList(Arrays.asList(values)); - } - - private void attachMoreExtras(Intent intent) { - for (Map.Entry entry : moreExtras.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue(); - // Kind of hacky - if (value instanceof Integer) { - intent.putExtra(key, (Integer) value); - } else if (value instanceof Long) { - intent.putExtra(key, (Long) value); - } else if (value instanceof Boolean) { - intent.putExtra(key, (Boolean) value); - } else if (value instanceof Double) { - intent.putExtra(key, (Double) value); - } else if (value instanceof Float) { - intent.putExtra(key, (Float) value); - } else if (value instanceof Bundle) { - intent.putExtra(key, (Bundle) value); - } else { - intent.putExtra(key, value.toString()); - } - } - } - -} diff --git a/app/src/main/java/com/google/zxing/integration/android/IntentResult.java b/app/src/main/java/com/google/zxing/integration/android/IntentResult.java deleted file mode 100644 index 9e452fb..0000000 --- a/app/src/main/java/com/google/zxing/integration/android/IntentResult.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2009 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.integration.android; - -/** - *

Encapsulates the result of a barcode scan invoked through {@link IntentIntegrator}.

- * - * @author Sean Owen - */ -public final class IntentResult { - - private final String contents; - private final String formatName; - private final byte[] rawBytes; - private final Integer orientation; - private final String errorCorrectionLevel; - - IntentResult() { - this(null, null, null, null, null); - } - - IntentResult(String contents, - String formatName, - byte[] rawBytes, - Integer orientation, - String errorCorrectionLevel) { - this.contents = contents; - this.formatName = formatName; - this.rawBytes = rawBytes; - this.orientation = orientation; - this.errorCorrectionLevel = errorCorrectionLevel; - } - - /** - * @return raw content of barcode - */ - public String getContents() { - return contents; - } - - /** - * @return name of format, like "QR_CODE", "UPC_A". See {@code BarcodeFormat} for more format names. - */ - public String getFormatName() { - return formatName; - } - - /** - * @return raw bytes of the barcode content, if applicable, or null otherwise - */ - public byte[] getRawBytes() { - return rawBytes; - } - - /** - * @return rotation of the image, in degrees, which resulted in a successful scan. May be null. - */ - public Integer getOrientation() { - return orientation; - } - - /** - * @return name of the error correction level used in the barcode, if applicable - */ - public String getErrorCorrectionLevel() { - return errorCorrectionLevel; - } - - @Override - public String toString() { - int rawBytesLength = rawBytes == null ? 0 : rawBytes.length; - return "Format: "+formatName+'\n'+"Contents: "+contents+'\n'+"Raw bytes: ("+rawBytesLength+" bytes)\n"+"Orientation: "+orientation+'\n'+"EC level: "+errorCorrectionLevel+'\n'; - } - -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java old mode 100644 new mode 100755 index 363be73..0c31def --- a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java @@ -1,509 +1,433 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid; import android.app.Activity; import android.app.AlertDialog; -import android.app.Dialog; -import android.content.ClipData; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; +import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; -import android.text.InputType; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.LinearLayout; -import android.widget.TextView; import android.widget.Toast; -import com.google.zxing.integration.android.IntentIntegrator; -import com.google.zxing.integration.android.IntentResult; +import java.util.ArrayList; -import java.math.BigInteger; -import java.security.SecureRandom; - -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; import de.vanitasvitae.enigmandroid.layout.LayoutContainer; -import de.vanitasvitae.enigmandroid.layout.PassphraseDialogBuilder; +/** + * Main Android Activity of the app + * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae + */ public class MainActivity extends Activity { - private static final int RESULT_SETTINGS = 1; - private static final String URI_CHANGELOG = - "https://github.com/vanitasvitae/EnigmAndroid/blob/master/CHANGELOG.txt"; - public static final String APP_ID = "EnigmAndroid"; - public static final int latest_protocol_version = 1; - public static final int max_protocol_version = 256; + private static final int RESULT_SETTINGS = 1; + private static final String URI_CHANGELOG = + "https://github.com/vanitasvitae/EnigmAndroid/blob/master/CHANGELOG.txt"; - private LayoutContainer layoutContainer; + LayoutContainer layoutContainer; + protected String prefMachineType; + protected boolean prefAnomaly; + protected String prefNumericLanguage; + protected String prefMessageFormatting; - private SecureRandom secureRandom; + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + Log.d("Activity","OnCreate!"); + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + this.prefMachineType = sharedPreferences.getString("prefMachineType", getResources(). + getStringArray(R.array.pref_list_machine_type)[0]); + ActivitySingleton singleton = ActivitySingleton.getInstance(); + singleton.setActivity(this); + updateContentView(); + layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType); + updatePreferenceValues(); + //Handle shared text + Intent intent = getIntent(); + String action = intent.getAction(); + String type = intent.getType(); - @Override - public void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - secureRandom = new SecureRandom(); - ActivitySingleton.getInstance().setActivity(this); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - SettingsActivity.SettingsSingleton.getInstance(prefs, getResources()); - layoutContainer = LayoutContainer.createLayoutContainer(); + if (Intent.ACTION_SEND.equals(action) && type != null) { + if ("text/plain".equals(type)) + { + String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); + if (sharedText != null) + { + layoutContainer.getInput().setRawText(sharedText); + } + } + } + } - //Handle shared text - Intent intent = getIntent(); - String action = intent.getAction(); - String type = intent.getType(); - if (Intent.ACTION_SEND.equals(action) && type != null) { - if ("text/plain".equals(type)) - { - String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); - if (sharedText != null) - { - //If shared text consists of an encoded configuration, try to restore it - if(sharedText.startsWith(APP_ID+"/")) - restoreStateFromCode(sharedText); - //Else put it in the input text box - else - layoutContainer.getInput().setRawText(sharedText); - } - } - } - } + /** + * Unfortunately we have to overwrite this, because on orientation changes the LayoutContainer + * will reset and we would lose information about plugboard, reflector-wiring and ring settings. + * These info are we saving here. + * TODO: Find more elegant solution + * @param outState state + */ + @Override + protected void onSaveInstanceState (Bundle outState) + { + ArrayList plugboard = new ArrayList<>(); + if(layoutContainer.getState().getConfigurationPlugboard() != null) { + for (int i : layoutContainer.getState().getConfigurationPlugboard()) plugboard.add(i); + } + outState.putIntegerArrayList("plugboard",plugboard); - public SecureRandom getSecureRandom() - { - return this.secureRandom; - } + ArrayList reflector = new ArrayList<>(); + if(layoutContainer.getState().getConfigurationReflector() != null) { + for (int i : layoutContainer.getState().getConfigurationReflector()) reflector.add(i); + } + outState.putIntegerArrayList("reflector", reflector); - public void onDialogFinished(EnigmaStateBundle state) - { - layoutContainer.getEnigma().setState(state); - } + outState.putInt("ring1", layoutContainer.getState().getRingSettingRotor1()); + outState.putInt("ring2", layoutContainer.getState().getRingSettingRotor2()); + outState.putInt("ring3", layoutContainer.getState().getRingSettingRotor3()); + outState.putInt("ring4", layoutContainer.getState().getRingSettingRotor4()); + outState.putInt("ringR", layoutContainer.getState().getRingSettingReflector()); - @Override - public boolean onCreateOptionsMenu(Menu menu) - { - this.getMenuInflater().inflate(R.menu.main, menu); - return true; - } + super.onSaveInstanceState(outState); + } - @Override - /** - * Handle Options menu clicks - */ - public boolean onOptionsItemSelected(MenuItem item) - { - int id = item.getItemId(); - if (id == R.id.action_reset) - { - layoutContainer.resetLayout(); - Toast.makeText(getApplicationContext(), R.string.message_reset, - Toast.LENGTH_SHORT).show(); - return true; - } - else if (id == R.id.action_send_message) - { - if(layoutContainer.getOutput().getText().length() == 0) - { - Toast.makeText(this, R.string.error_no_text_to_send, Toast.LENGTH_SHORT).show(); - } - else - { - Intent sendIntent = new Intent(); - sendIntent.setAction(Intent.ACTION_SEND); - sendIntent.putExtra(Intent.EXTRA_TEXT, layoutContainer.getOutput().getModifiedText()); - sendIntent.setType("text/plain"); - startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); - } - } - else if (id == R.id.action_choose_ringsetting) - { - layoutContainer.showRingSettingsDialog(); - return true; - } - else if(id == R.id.action_share_configuration) - { - showShareConfigurationDialog(); - } - else if (id == R.id.action_restore_configuration) - { - showReceiveConfigurationDialog(); - return true; - } - else if (id == R.id.action_random_configuration) - { - layoutContainer.getEnigma().randomState(); - layoutContainer.syncStateFromEnigmaToLayout(); - Toast.makeText(getApplicationContext(), R.string.message_random, - Toast.LENGTH_SHORT).show(); - layoutContainer.getOutput().setText(""); - return true; - } - else if (id == R.id.action_settings) - { - Intent i = new Intent(this, SettingsActivity.class); - startActivityForResult(i, RESULT_SETTINGS); - } - else if (id == R.id.action_about) - { - showAboutDialog(); - return true; - } - return super.onOptionsItemSelected(item); - } + /** + * Here we get back values previously saved int onSaveInstanceState + * @param savedInstanceState state + */ + @Override + protected void onRestoreInstanceState (Bundle savedInstanceState) + { + ArrayList plugboard = savedInstanceState.getIntegerArrayList("plugboard"); + if(plugboard != null && plugboard.size() != 0) { + int[] p = new int[plugboard.size()]; + for (int i = 0; i < plugboard.size(); i++) p[i] = plugboard.get(i); + layoutContainer.getState().setConfigurationPlugboard(p); + } - /** - * Set the chosen Configuration to the enigma, get the input string from the input text box and - * prepare it, set the input to the prepared text, encrypt the prepared input and set the - * encrypted string to the output text box and update the spinners to their new positions. - * @param v View - */ - public void doCrypto(View v) - { - layoutContainer.doCrypto(); - } + ArrayList reflector = savedInstanceState.getIntegerArrayList("reflector"); + if(reflector != null && reflector.size() != 0) { + int[] r = new int[reflector.size()]; + for (int i = 0; i < reflector.size(); i++) r[i] = reflector.get(i); + layoutContainer.getState().setConfigurationReflector(r); + } - /** - * Start an intent to share the configuration as QR-Code via Barcode Scanner - */ - private void shareConfigurationAsQR() - { - IntentIntegrator QRIntegrator = new IntentIntegrator(this); - layoutContainer.syncStateFromLayoutToEnigma(); - String encoded_state = APP_ID+"/"+layoutContainer.getEnigma().getEncodedState().toString(16); - Log.d(APP_ID, "Sharing configuration to QR: "+encoded_state); - QRIntegrator.shareText(encoded_state); - } + layoutContainer.getState().setRingSettingRotor1(savedInstanceState.getInt("ring1")); + layoutContainer.getState().setRingSettingRotor2(savedInstanceState.getInt("ring2")); + layoutContainer.getState().setRingSettingRotor3(savedInstanceState.getInt("ring3")); + layoutContainer.getState().setRingSettingRotor4(savedInstanceState.getInt("ring4")); + layoutContainer.getState().setRingSettingReflector(savedInstanceState.getInt("ringR")); + } - /** - * Start an intent to share the configuration as text - */ - private void shareConfigurationAsText() - { - Intent sendIntent = new Intent(); - sendIntent.setAction(Intent.ACTION_SEND); - sendIntent.putExtra(Intent.EXTRA_TEXT, - APP_ID+"/"+layoutContainer.getEnigma().getEncodedState().toString(16)); - sendIntent.setType("text/plain"); - startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); - } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + this.updateContentView(); + } - /** - * Start the barcode app to scan a barcode for configuration - */ - private void receiveConfigurationQR() - { - IntentIntegrator integrator = new IntentIntegrator(this); - integrator.initiateScan(); - } + private void updateContentView() + { + switch (prefMachineType) + { + case "I": + this.setContentView(R.layout.activity_main_i_m3); + break; + case "M3": + this.setContentView(R.layout.activity_main_i_m3); + break; + case "M4": + this.setContentView(R.layout.activity_main_m4); + break; + case "D": + this.setContentView(R.layout.activity_main_d); + break; + case "K": + this.setContentView(R.layout.activity_main_k_t); + break; + case "T": + this.setContentView(R.layout.activity_main_k_t); + break; + default: + this.setContentView(R.layout.activity_main_i_m3); + break; + } + } - /** - * Show a dialog to restore a configuration - */ - private void receiveConfigurationText() - { - new PassphraseDialogBuilder().showDialog(); - } + private void updatePreferenceValues() + { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + this.setPrefMachineType(sharedPreferences.getString("prefMachineType", getResources(). + getStringArray(R.array.pref_list_machine_type)[0])); + this.setPrefAnomaly(sharedPreferences.getBoolean("prefAnomaly", true)); + this.setPrefNumericLanguage(sharedPreferences.getString("prefNumericLanguage", getResources(). + getStringArray(R.array.pref_alias_numeric_spelling_language)[0])); + this.setPrefMessageFormatting(sharedPreferences.getString("prefMessageFormatting", getResources(). + getStringArray(R.array.pref_alias_message_formatting)[0])); + } - /** - * Show a Dialog containing information about the app, license, usage, author and a link - * to the changelog - */ - private void showAboutDialog() - { - final View aboutView = View.inflate(this, R.layout.dialog_about, null); - //Get and set Version code - PackageInfo pInfo = null; - try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);} - catch (PackageManager.NameNotFoundException e){ e.printStackTrace();} - assert pInfo != null; - String version = pInfo.versionName+ " ("+pInfo.versionCode+")"; - TextView versionText = (TextView) aboutView.findViewById(R.id.about_version_section); - versionText.setText(version); + public void setPrefMachineType(String type) + { + if(prefMachineType == null || !prefMachineType.equals(type)) + { + prefMachineType = type; + String savedInput = ""; + if(layoutContainer != null) + { + savedInput = layoutContainer.getInput().getText(); + } + updateContentView(); + layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType); + layoutContainer.setInputPreparer(InputPreparer.createInputPreparer(prefNumericLanguage)); + layoutContainer.getInput().setText(savedInput); + } + } - //Build and show dialog - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.title_about_dialog); - builder.setView(aboutView) - .setCancelable(true) - .setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int id) - { - dialog.dismiss(); - } - }) - .setNegativeButton(R.string.button_show_changelog, new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int id) - { - dialog.cancel(); - openWebPage(URI_CHANGELOG); - } - }).show(); - } + public String getPrefMachineType() + { + if(prefMachineType != null) return prefMachineType; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + this.prefMachineType = sharedPreferences.getString("prefMachineType", getResources(). + getStringArray(R.array.pref_list_machine_type)[0]); + return prefMachineType; + } - /** - * Show a dialog where the user can choose between sharing the configuration via QR-code or - * via string (intent or copy-to-clipboard) - */ - private void showShareConfigurationDialog() - { - final String configuration = APP_ID+"/"+layoutContainer.getEnigma().getEncodedState().toString(16); - final View shareView = View.inflate(this, R.layout.dialog_two_options, null); - LinearLayout l = (LinearLayout) shareView.findViewById(R.id.dialog_two_options_lay); - EditText e = new EditText(this); - e.setText(configuration); - e.setInputType(InputType.TYPE_NULL); - e.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){ - android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); - ClipData clip; - clip = ClipData.newPlainText("label", configuration); - clipboard.setPrimaryClip(clip); - } else{ - @SuppressWarnings("deprecation") - android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE); - clipboard.setText(configuration); - } - Toast.makeText(getApplicationContext(), R.string.message_clipboard, Toast.LENGTH_SHORT).show(); - } - }); - l.addView(e); - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.dialog_title_share_configuration) - .setView(shareView).setCancelable(true); - final Dialog d = builder.create(); - Button one = (Button) shareView.findViewById(R.id.dialog_two_options_1); - one.setText(R.string.dialog_share_qr); - one.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - shareConfigurationAsQR(); - d.dismiss(); - } - }); - Button two = (Button) shareView.findViewById(R.id.dialog_two_options_2); - two.setText(R.string.dialog_share_code); - two.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - shareConfigurationAsText(); - d.dismiss(); - } - }); - d.show(); - } + public void setPrefAnomaly(boolean anomaly) + { + if(prefAnomaly !=anomaly) + { + prefAnomaly = anomaly; + if(layoutContainer != null) layoutContainer.getEnigma().setPrefAnomaly(anomaly); + } + } - /** - * Show a dialog, where the user can choose between scanning QR-code and entering a string to - * restore the encoded configuration - */ - private void showReceiveConfigurationDialog() - { - final View shareView = View.inflate(this, R.layout.dialog_two_options, null); - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.dialog_title_restore_configuration) - .setView(shareView).setCancelable(true); - final Dialog d = builder.create(); - Button one = (Button) shareView.findViewById(R.id.dialog_two_options_1); - one.setText(R.string.dialog_restore_qr); - one.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - receiveConfigurationQR(); - d.dismiss(); - } - }); - Button two = (Button) shareView.findViewById(R.id.dialog_two_options_2); - two.setText(R.string.dialog_restore_code); - two.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - receiveConfigurationText(); - d.dismiss(); - } - }); - d.show(); - } + public boolean getPrefAnomaly() + { + return prefAnomaly; + } - /** - * Handle Activity Results - * @param requestCode requestCode - * @param resultCode resultCode (RESULT_SETTINGS is defined at the top) - * @param data data - */ - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - switch (requestCode) { - //Come back from Settings - case RESULT_SETTINGS: - { - applyPreferenceChanges(); - break; - } - // Receive from QR - case IntentIntegrator.REQUEST_CODE: - IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); - if (scanResult != null) { - String content = scanResult.getContents(); - if(content == null) Log.e(APP_ID, "Error! Received nothing from QR-Code!"); - else { - Log.d(APP_ID, "Received " + content + " from QR-Code!"); - restoreStateFromCode(content); - } - } - } - } + public void setPrefNumericLanguage(String lang) + { + if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang)) + { + prefNumericLanguage = lang; + layoutContainer.setInputPreparer(InputPreparer.createInputPreparer(lang)); + } + } - /** - * Handle changes in preferences and apply those changes to the app - */ - private void applyPreferenceChanges() - { - SettingsActivity s = SettingsActivity.SettingsSingleton.getInstance(); - if(s.prefMachineTypeChanged()) - { - layoutContainer = LayoutContainer.createLayoutContainer(); - } - if(s.prefMessageFormattingChanged()) - { - layoutContainer.setEditTextAdapter(s.getPrefMessageFormatting()); - } - if(s.prefNumericLanguageChanged()) - { - layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); - } - } + public String getPrefNumericLanguage() + { + if(prefNumericLanguage != null) return prefNumericLanguage; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + this.prefNumericLanguage = sharedPreferences.getString("prefNumericLanguage", getResources(). + getStringArray(R.array.pref_alias_numeric_spelling_language)[0]); + return prefNumericLanguage; + } - /** - * Set EnigmAndroid into a certain state as described in the QR-Code - * @param mem content of the QR-Code - */ - public void restoreStateFromCode(String mem) - { - if(!mem.startsWith(APP_ID+"/")) - { - Toast.makeText(this, R.string.error_no_valid_qr, Toast.LENGTH_LONG).show(); - } - else - { - String inputString = layoutContainer.getInput().getText(); - mem = mem.substring((APP_ID+"/").length()); - BigInteger s = new BigInteger(mem, 16); - int protocol_version = Enigma.getValue(s, max_protocol_version); - s = Enigma.removeDigit(s, max_protocol_version); - Log.d(APP_ID, - "Try to restore configuration from BigInteger value "+s.toString()+" in protocol version "+protocol_version+"."); - SettingsActivity.SettingsSingleton.getInstance() - .setPrefMachineType(Enigma.chooseEnigmaFromSave(s)); - layoutContainer = LayoutContainer.createLayoutContainer(); - layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20), protocol_version); - layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); - layoutContainer.syncStateFromEnigmaToLayout(); - layoutContainer.getInput().setText(inputString); - layoutContainer.getOutput().setText(""); - } - } + public void setPrefMessageFormatting(String messageFormatting) + { + if(prefMessageFormatting == null || !prefMessageFormatting.equals(messageFormatting)) + { + prefMessageFormatting = messageFormatting; + layoutContainer.setEditTextAdapter(messageFormatting); + } + } - /** - * Set EnigmAndroid into a state calculated from the seed. - * @param seed seed - */ - public void applyStateFromSeed(String seed) - { - String inputString = layoutContainer.getInput().getText(); - SettingsActivity.SettingsSingleton.getInstance() - .setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed)); - layoutContainer = LayoutContainer.createLayoutContainer(); - layoutContainer.getEnigma().setStateFromSeed(seed); - layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); - layoutContainer.syncStateFromEnigmaToLayout(); - layoutContainer.getInput().setText(inputString); - layoutContainer.getOutput().setText(""); - } + public String getPrefMessageFormatting() + { + if(prefMessageFormatting != null) return prefMessageFormatting; + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + this.prefMessageFormatting = sharedPreferences.getString("prefMessageFormatting", getResources(). + getStringArray(R.array.pref_alias_message_formatting)[0]); + return prefMessageFormatting; + } - /** - * Open the web page with the URL url - * @param url URL of the website - */ - private void openWebPage(String url) { - Uri webPage = Uri.parse(url); - Intent intent = new Intent(Intent.ACTION_VIEW, webPage); - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(intent); - } - } + @Override + public boolean onCreateOptionsMenu(Menu menu) + { + this.getMenuInflater().inflate(R.menu.main, menu); + return true; + } - /** - * Singleton that grants access to an Activity from anywhere within the app - */ - public static class ActivitySingleton - { - private static ActivitySingleton instance = null; - private Activity activity; + @Override + /** + * Handle Options menu clicks + */ + public boolean onOptionsItemSelected(MenuItem item) + { + int id = item.getItemId(); + if (id == R.id.action_reset) + { + layoutContainer.resetLayout(); + Toast.makeText(getApplicationContext(), R.string.message_reset, + Toast.LENGTH_SHORT).show(); + return true; + } + else if (id == R.id.action_choose_ringstellung) + { + layoutContainer.showRingSettingsDialog(); + return true; + } + else if (id == R.id.action_settings) + { + Intent i = new Intent(this, SettingsActivity.class); + startActivityForResult(i, RESULT_SETTINGS); + } + else if (id == R.id.action_about) + { + showAboutDialog(); + return true; + } + else if (id == R.id.action_send) + { + if(layoutContainer.getOutput().getText().length() == 0) + { + Toast.makeText(this, R.string.error_no_text_to_send, Toast.LENGTH_SHORT).show(); + } + else + { + Intent sendIntent = new Intent(); + sendIntent.setAction(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_TEXT, layoutContainer.getOutput().getModifiedText()); + sendIntent.setType("text/plain"); + startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); + } + } + return super.onOptionsItemSelected(item); + } - //private constructor - private ActivitySingleton(){} - //Singleton method - public static ActivitySingleton getInstance() - { - if(instance == null) instance = new ActivitySingleton(); - return instance; - } + /** + * Set the chosen Configuration to the enigma, get the input string from the input textbox and + * prepare it, set the input to the prepared text, encrypt the prepared input and set the + * encrypted string to the output textbox and update the spinners to their new positions. + * @param v View + */ + public void doCrypto(View v) + { + layoutContainer.doCrypto(); + } - /** - * Set an Activity that the Singleton returns - * @param activity activity that's stored - */ - public void setActivity(Activity activity) - { - this.activity = activity; - } + /** + * Show a Dialog containing information about the app, license, usage, author and a link + * to the changelog + */ + public void showAboutDialog() + { + final View aboutView = View.inflate(this, R.layout.dialog_about, null); - /** - * Returns the stored Activity - * @return stored Activity - */ - public Activity getActivity() - { - return activity; - } + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.title_about_dialog); + builder.setView(aboutView) + .setCancelable(true) + .setPositiveButton(R.string.dialog_positiv, new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int id) + { + dialog.dismiss(); + } + }) + .setNegativeButton(R.string.button_show_changelog, new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int id) + { + dialog.cancel(); + openWebPage(URI_CHANGELOG); + } + }).show(); + } + + /** + * Handle preference changes + * @param requestCode requestCode + * @param resultCode resultCode (RESULT_SETTINGS is defined at the top) + * @param data data (not important here) + */ + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + switch (requestCode) { + case RESULT_SETTINGS: + { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); + this.setPrefMachineType(sharedPrefs.getString("prefMachineType", getResources() + .getStringArray(R.array.pref_list_machine_type)[0])); + this.setPrefAnomaly(sharedPrefs.getBoolean("prefAnomaly", true)); + this.setPrefNumericLanguage(sharedPrefs.getString("prefNumericLanguage", getResources(). + getStringArray(R.array.pref_alias_numeric_spelling_language)[0])); + this.setPrefMessageFormatting(sharedPrefs.getString("prefMessageFormatting", + getResources().getStringArray(R.array.pref_alias_message_formatting)[0])); + break; + } + + } + } + + /** + * Open the web page with the URL url + * @param url URL of the website + */ + public void openWebPage(String url) { + Uri webPage = Uri.parse(url); + Intent intent = new Intent(Intent.ACTION_VIEW, webPage); + if (intent.resolveActivity(getPackageManager()) != null) { + startActivity(intent); + } + } + +/** + * Singleton that grants access to an Activity from anywhere within the app + */ +public static class ActivitySingleton +{ + private static ActivitySingleton instance = null; + private Activity activity; + + //private constructor + private ActivitySingleton(){} + //Singleton method + public static ActivitySingleton getInstance() + { + if(instance == null) instance = new ActivitySingleton(); + return instance; + } + + /** + * Set an Activity that the Singleton returns + * @param activity activity that's stored + */ + public void setActivity(Activity activity) + { + this.activity = activity; + } + + /** + * Returns the stored Activity + * @return stored Activity + */ + public Activity getActivity() + { + return activity; + } - } +} } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java b/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java old mode 100644 new mode 100755 index 4b36f3f..eaa4be6 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java @@ -1,254 +1,34 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid; -import android.content.SharedPreferences; -import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceActivity; -import android.util.Log; /** * Class that represents the settings activity. - * Use the singleton to get an instance of this class to get preferences + * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + *@author vanitasvitae */ public class SettingsActivity extends PreferenceActivity { - public static final String PREF_NUMERIC_LANGUAGE = "prefNumericLanguage"; - public static final String PREF_MACHINE_TYPE = "prefMachineType"; - public static final String PREF_MESSAGE_FORMATTING = "prefMessageFormatting"; - public static final String PREF_REPLACE_SPECIAL_CHARACTERS = "prefReplaceSpecialCharacters"; - public static final String PREF_SAVED_ENIGMA_STATE = "prefSavedEnigmaState"; - public static final String PREF_VERSION_NUMBER = "prefVersionNumber"; - - private String previousPrefNumericLanguage; - private String previousPrefMachineType; - private String previousPrefMessageFormatting; - private boolean previousPrefReplaceSpecialCharacters; - private String previousPrefSavedEnigmaState; - - private SharedPreferences prefs; - private Resources res; - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - //noinspection deprecation - addPreferencesFromResource(R.xml.pref_page); - } - - public void setSharedPreferences(SharedPreferences prefs) - { - this.prefs = prefs; - } - - public void setResources(Resources res) - { - this.res = res; - } - - private boolean isFullyInitialized() - { - if (prefs == null) - { - Log.e(MainActivity.APP_ID, - "SharedPreferences not initialized via setSharedPreferences!"); - return false; - } - if (res == null) - { - Log.e(MainActivity.APP_ID, "Resources not initialized via setResources!"); - return false; - } - return true; - } - - public String getPrefNumericLanguage() - { - if (isFullyInitialized()) - return prefs.getString( - PREF_NUMERIC_LANGUAGE, - res.getStringArray(R.array.pref_alias_message_formatting)[0]); - else return null; - } - - public void setPrefNumericLanguage(String lang) - { - if (isFullyInitialized()) - prefs.edit().putString(PREF_NUMERIC_LANGUAGE, lang).apply(); - } - - public boolean prefNumericLanguageChanged() - { - if (this.previousPrefNumericLanguage == null || !this.previousPrefNumericLanguage.equals( - getPrefNumericLanguage())) - { - this.previousPrefNumericLanguage = this.getPrefNumericLanguage(); - Log.d(MainActivity.APP_ID, PREF_NUMERIC_LANGUAGE+" changed!"); - return true; - } - return false; - } - - /** - * Return whether special characters will be replaced. - * If the SettingsActivity is not fully initialized return false and ignore preference. - * @return boolean - */ - public boolean getPrefReplaceSpecialCharacters() - { - return isFullyInitialized() && prefs.getBoolean(PREF_REPLACE_SPECIAL_CHARACTERS, true); - } - - public void setPrefReplaceSpecialCharacters(boolean replace) - { - if (isFullyInitialized()) - prefs.edit().putBoolean(PREF_REPLACE_SPECIAL_CHARACTERS, replace).apply(); - } - - public boolean prefReplaceSpecialCharactersChanged() - { - boolean changed = previousPrefReplaceSpecialCharacters != getPrefReplaceSpecialCharacters(); - if (changed) - { - previousPrefReplaceSpecialCharacters = getPrefReplaceSpecialCharacters(); - Log.d(MainActivity.APP_ID, PREF_REPLACE_SPECIAL_CHARACTERS+" changed!"); - return true; - } - return false; - } - - public String getPrefMachineType() - { - if (isFullyInitialized()) - return prefs.getString(PREF_MACHINE_TYPE, - res.getStringArray(R.array.pref_alias_machine_type)[0]); - else return null; - } - - public void setPrefMachineType(String pref) - { - if (isFullyInitialized()) - prefs.edit().putString(PREF_MACHINE_TYPE, pref).apply(); - } - - public boolean prefMachineTypeChanged() - { - if (this.previousPrefMachineType == null || !this.previousPrefMachineType.equals( - getPrefMachineType())) - { - this.previousPrefMachineType = this.getPrefMachineType(); - Log.d(MainActivity.APP_ID, PREF_MACHINE_TYPE+" changed!"); - return true; - } - return false; - } - - public String getPrefSavedEnigmaState() - { - if (isFullyInitialized()) - return prefs.getString(PREF_SAVED_ENIGMA_STATE, "-1"); - else return null; - } - - /** - * @param state HEX - */ - public void setPrefSavedEnigmaState(String state) - { - if (isFullyInitialized()) - prefs.edit().putString(PREF_SAVED_ENIGMA_STATE, state).apply(); - } - - public boolean prefSavedEnigmaStateChanged() - { - if (this.previousPrefSavedEnigmaState == null || !this.previousPrefSavedEnigmaState - .equals(getPrefSavedEnigmaState())) - { - this.previousPrefSavedEnigmaState = this.getPrefSavedEnigmaState(); - Log.d(MainActivity.APP_ID, PREF_SAVED_ENIGMA_STATE+" changed!"); - return true; - } - return false; - } - - public String getPrefMessageFormatting() - { - if (isFullyInitialized()) - return prefs.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, - res.getStringArray(R.array.pref_alias_message_formatting)[0]); - else return null; - } - - public void setPrefMessageFormatting(String format) - { - if (isFullyInitialized()) - prefs.edit().putString(PREF_MESSAGE_FORMATTING, format).apply(); - } - - public boolean prefMessageFormattingChanged() - { - if (this.previousPrefMessageFormatting == null || !this.previousPrefMessageFormatting - .equals(getPrefMessageFormatting())) - { - this.previousPrefMessageFormatting = this.getPrefMessageFormatting(); - Log.d(MainActivity.APP_ID, PREF_MESSAGE_FORMATTING+" changed!"); - return true; - } - return false; - } - - public int getVersionNumber() - { - if (isFullyInitialized()) - return prefs.getInt(PREF_VERSION_NUMBER, -1); - else return -1; - } - - public void setVersionNumber(int v) - { - if (isFullyInitialized()) - prefs.edit().putInt(PREF_VERSION_NUMBER, v).apply(); - } - - public static class SettingsSingleton extends SettingsActivity - { - private static SettingsActivity instance; - - private SettingsSingleton() - { - super(); - } - - public static SettingsActivity getInstance(SharedPreferences prefs, Resources res) - { - instance = new SettingsActivity(); - instance.setSharedPreferences(prefs); - instance.setResources(res); - return instance; - } - - public static SettingsActivity getInstance() - { - if (instance == null) - { - instance = new SettingsActivity(); - Log.d(MainActivity.APP_ID, "Created new SettingsActivity!"); - } - return instance; - } - } -} \ No newline at end of file + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + //noinspection deprecation + addPreferencesFromResource(R.xml.pref_page); + } +} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java old mode 100644 new mode 100755 index 40327b4..2b99936 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java @@ -1,291 +1,108 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; -import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Random; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - /** * Main component of the Enigma machine * This is the mostly abstract base of any enigma machine. * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public abstract class Enigma { - static String machineType; + protected static String machineType; + protected boolean doAnomaly = false; //Has the time come to handle an anomaly? + protected boolean prefAnomaly; //Do you WANT to simulate the anomaly? - boolean doAnomaly = false; //Has the time come to handle an anomaly? + public Enigma() + { + initialize(); + } - ArrayList availableEntryWheels; - ArrayList availableRotors; - ArrayList availableReflectors; + /** + * Set the enigma to an initial state + */ + public abstract void initialize(); - Random rand; + /** + * Encrypt / Decrypt a given String w. + * w must be prepared using prepare(w) beforehand. + * Doing so changes the state of the rotors but not the state of the plugboard and the + * ringSettings + * + * @param w Text to decrypt/encryptString + * @return encrypted/decrypted string + */ + public String encryptString(String w) + { + //output string + String output = ""; + //for each char x in k + for (char x : w.toCharArray()) + { + output = output + this.encryptChar(x); + } + //return en-/decrypted string + return output; + } - Enigma() - { - establishAvailableParts(); - initialize(); - } + /** + * Set the enigma into the next mechanical state. + * This rotates the first rotor and eventually also the second/third. + * Also this method handles the anomaly in case it should happen. + */ + public abstract void nextState(); - /** - * In this method, available EntryWheels, Rotors and Reflectors can be defined. - */ - protected abstract void establishAvailableParts(); + /** + * Substitute char k by sending the signal through the enigma. + * The signal passes the plugboard, the rotors and returns back after going through the + * reflector wheel. + * + * @param k input char + * @return substituted output char + */ + public abstract char encryptChar(char k); - /** - * Add a Rotor to the ArrayList of available rotors for this machine. - * Also set the index of the Rotor. - * @param r Rotor - */ - void addAvailableRotor(Rotor r) - { - if(availableRotors == null) availableRotors = new ArrayList<>(); - availableRotors.add(availableRotors.size(), r.setIndex(availableRotors.size())); - } + /** + * Set the state of the enigma + * @param state new state + */ + public abstract void setState(EnigmaStateBundle state); - void addAvailableEntryWheel(EntryWheel e) - { - if(availableEntryWheels == null) availableEntryWheels = new ArrayList<>(); - availableEntryWheels.add(availableEntryWheels.size(), e.setIndex(availableEntryWheels.size())); - } + /** + * Return an object representing the current state of the enigma + * @return state + */ + public abstract EnigmaStateBundle getState(); - void addAvailableReflector(Reflector r) - { - if(availableReflectors == null) availableReflectors = new ArrayList<>(); - availableReflectors.add(availableReflectors.size(), r.setIndex(availableReflectors.size())); - } + /** + * set prefAnomaly variable + * @param b boolean + */ + public void setPrefAnomaly(boolean b) + { + this.prefAnomaly = b; + } - public ArrayList getAvailableEntryWheels() - { - return availableEntryWheels; - } + /** + * Return the type indicator of the enigma machine + * @return type + */ + public String getMachineType() + { + return machineType; + } - public ArrayList getAvailableRotors() - { - return availableRotors; - } - - public ArrayList getAvailableReflectors() - { - return availableReflectors; - } - - EntryWheel getEntryWheel(int index) - { - if(availableEntryWheels == null || availableEntryWheels.size() == 0) return null; - return availableEntryWheels.get(index % availableEntryWheels.size()).getInstance(); - } - - Rotor getRotor(int index) - { - if(availableRotors == null || availableRotors.size() == 0) return null; - return availableRotors.get(index % availableRotors.size()).getInstance(); - } - - Rotor getRotor(int index, int rotation, int ringSetting) - { - return getRotor(index).setRotation(rotation).setRingSetting(ringSetting); - } - - Reflector getReflector(int index) - { - if(availableReflectors == null || availableReflectors.size() == 0) return null; - return availableReflectors.get(index%availableReflectors.size()).getInstance(); - } - - Reflector getReflector(int index, int rotation, int ringSetting) - { - return getReflector(index).setRotation(rotation).setRingSetting(ringSetting); - } - - /** - * Set the enigma to an initial state - */ - protected abstract void initialize(); - - /** - * Encrypt / Decrypt a given String w. - * w must be prepared using prepare(w) beforehand. - * Doing so changes the state of the rotors but not the state of the plugboard and the - * ringSettings - * - * @param w Text to decrypt/encryptString - * @return encrypted/decrypted string - */ - public String encryptString(String w) - { - //output string - String output = ""; - //for each char x in k - for (char x : w.toCharArray()) - { - output = output + this.encryptChar(x); - } - //return en-/decrypted string - return output; - } - - /** - * Set the enigma into the next mechanical state. - * This rotates the first rotor and eventually also the second/third. - * Also this method handles the anomaly in case it should happen. - */ - public abstract void nextState(); - - /** - * Set the enigma into a completely random state using a unseeded SecureRandom object. - */ - public void randomState() - { - this.rand = ((MainActivity) (MainActivity.ActivitySingleton.getInstance().getActivity())) - .getSecureRandom(); - generateState(); - } - - /** - * Set the enigma to a random state based on the initialization of rand. - * Don not choose a rotor twice, set random rotations, ringSettings, ukw and possibly - * plugboard / rewirable ukw configurations. - */ - protected abstract void generateState(); - - /** - * Substitute char k by sending the signal through the enigma. - * The signal passes the plugboard, the rotors and returns back after going through the - * reflector wheel. - * - * @param k input char - * @return substituted output char - */ - protected abstract char encryptChar(char k); - - /** - * Set the state of the enigma - * @param state new state - */ - public abstract void setState(EnigmaStateBundle state); - - /** - * Return an object representing the current state of the enigma - * @return state - */ - public abstract EnigmaStateBundle getState(); - - /** - * Set the rand into a certain state based on seed. - * Then set the enigmas state. - * @param seed passphrase - */ - public void setStateFromSeed(String seed) - { - rand = new Random(seed.hashCode()); - generateState(); - } - - public abstract void restoreState(BigInteger mem, int protocol_version); - - public BigInteger getEncodedState() - { - return getEncodedState(MainActivity.latest_protocol_version); - } - protected abstract BigInteger getEncodedState(int protocol_version); - - private static String numToMachineType(int n) - { - int m = 13; - n = (m+(n+m)%m)%m; //Problem? Trolololo - switch (n) { - case 0: return "I"; - case 1: return "M3"; - case 2: return "M4"; - case 3: return "G31"; - case 4: return "G312"; - case 5: return "G260"; - case 6: return "D"; - case 7: return "K"; - case 8: return "KS"; - case 9: return "KSA"; - case 10: return "R"; - case 11: return "T"; - case 12: return "KD"; - default: return "KD"; - } - } - - public static String chooseEnigmaFromSeed(String seed) - { - return numToMachineType(seed.hashCode() % 13); - } - - public static String chooseEnigmaFromSave(BigInteger save) - { - return numToMachineType(getValue(save, 20)); - } - - /** - * Return the name indicator of the enigma machine - * @return name - */ - public String getMachineType() - { - return machineType; - } - - /** - * - * @param s source - * @param d domain (max value) of the value - * @return value - */ - public static int getValue(BigInteger s, int d) - { - BigInteger o = s.mod(BigInteger.valueOf(d)).add(BigInteger.valueOf(d)).mod(BigInteger.valueOf(d)); - return Integer.valueOf(o.toString()); - } - - /** - * remove a digit of domain d from source s - * @param s source - * @param d domain (max value) - * @return trimmed source - */ - public static BigInteger removeDigit(BigInteger s, int d) - { - s = s.subtract(s.mod(BigInteger.valueOf(d))); - s = s.divide(BigInteger.valueOf(d)); - return s; - } - - /** - * - * @param s source - * @param b base (max value) - * @param v actual value - * @return lengthened source - */ - public static BigInteger addDigit(BigInteger s, int v, int b) - { - s = s.multiply(BigInteger.valueOf(b)); - s = s.add(BigInteger.valueOf(v % b)); - return s; - } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/EnigmaStateBundle.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/EnigmaStateBundle.java index 98a8261..1cf007b 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/EnigmaStateBundle.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/EnigmaStateBundle.java @@ -1,43 +1,54 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; /** * Class that contains a possible state of an enigma machine. * Used to store and transport configuration and settings. * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class EnigmaStateBundle{ - private String machineType; - private int typeEntryWheel; private int typeRotor1; private int typeRotor2; private int typeRotor3; private int typeRotor4; + private int typeRotor5; + private int typeRotor6; + private int typeRotor7; + private int typeRotor8; private int rotationRotor1; private int rotationRotor2; private int rotationRotor3; private int rotationRotor4; + private int rotationRotor5; + private int rotationRotor6; + private int rotationRotor7; + private int rotationRotor8; private int ringSettingRotor1; private int ringSettingRotor2; private int ringSettingRotor3; private int ringSettingRotor4; + private int ringSettingRotor5; + private int ringSettingRotor6; + private int ringSettingRotor7; + private int ringSettingRotor8; private int typeReflector; @@ -48,20 +59,6 @@ public class EnigmaStateBundle{ private int[] configurationReflector; -// --Commented out by Inspection START (07.11.15 19:46): -// public String getMachineType() -// { -// return this.machineType; -// } -// --Commented out by Inspection STOP (07.11.15 19:46) - -// --Commented out by Inspection START (07.11.15 19:46): -// public void setMachineType(String type) -// { -// this.machineType = type; -// } -// --Commented out by Inspection STOP (07.11.15 19:46) - public int getTypeRotor1() { return typeRotor1; } @@ -94,6 +91,38 @@ public class EnigmaStateBundle{ this.typeRotor4 = typeRotor4; } + public int getTypeRotor5() { + return typeRotor5; + } + + public void setTypeRotor5(int typeRotor5) { + this.typeRotor5 = typeRotor5; + } + + public int getTypeRotor6() { + return typeRotor6; + } + + public void setTypeRotor6(int typeRotor6) { + this.typeRotor6 = typeRotor6; + } + + public int getTypeRotor7() { + return typeRotor7; + } + + public void setTypeRotor7(int typeRotor7) { + this.typeRotor7 = typeRotor7; + } + + public int getTypeRotor8() { + return typeRotor8; + } + + public void setTypeRotor8(int typeRotor8) { + this.typeRotor8 = typeRotor8; + } + public int getRotationRotor1() { return rotationRotor1; } @@ -126,6 +155,38 @@ public class EnigmaStateBundle{ this.rotationRotor4 = rotationRotor4; } + public int getRotationRotor5() { + return rotationRotor5; + } + + public void setRotationRotor5(int rotationRotor5) { + this.rotationRotor5 = rotationRotor5; + } + + public int getRotationRotor6() { + return rotationRotor6; + } + + public void setRotationRotor6(int rotationRotor6) { + this.rotationRotor6 = rotationRotor6; + } + + public int getRotationRotor7() { + return rotationRotor7; + } + + public void setRotationRotor7(int rotationRotor7) { + this.rotationRotor7 = rotationRotor7; + } + + public int getRotationRotor8() { + return rotationRotor8; + } + + public void setRotationRotor8(int rotationRotor8) { + this.rotationRotor8 = rotationRotor8; + } + public int getRingSettingRotor1() { return ringSettingRotor1; } @@ -158,6 +219,38 @@ public class EnigmaStateBundle{ this.ringSettingRotor4 = ringSettingRotor4; } + public int getRingSettingRotor5() { + return ringSettingRotor5; + } + + public void setRingSettingRotor5(int ringSettingRotor5) { + this.ringSettingRotor5 = ringSettingRotor5; + } + + public int getRingSettingRotor6() { + return ringSettingRotor6; + } + + public void setRingSettingRotor6(int ringSettingRotor6) { + this.ringSettingRotor6 = ringSettingRotor6; + } + + public int getRingSettingRotor7() { + return ringSettingRotor7; + } + + public void setRingSettingRotor7(int ringSettingRotor7) { + this.ringSettingRotor7 = ringSettingRotor7; + } + + public int getRingSettingRotor8() { + return ringSettingRotor8; + } + + public void setRingSettingRotor8(int ringSettingRotor8) { + this.ringSettingRotor8 = ringSettingRotor8; + } + public int getTypeReflector() { return typeReflector; } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java index 1cfa89e..d781715 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_D.java @@ -1,221 +1,129 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Plugboard; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Concrete implementation of an enigma machine of name D - * This machine has a rewirable UKW, non changeable rotors. + * Concrete implementation of an enigma machine of type I * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class Enigma_D extends Enigma { + protected Rotor entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; - private EntryWheel entryWheel; - private Rotor rotor1; - private Rotor rotor2; - private Rotor rotor3; - private Reflector reflector; + protected Reflector.ReflectorEnigmaDKD reflector; - public Enigma_D() - { - super(); - machineType = "D"; - Log.d(MainActivity.APP_ID, "Created Enigma D"); - } + public Enigma_D() + { + super(); + machineType = "D"; + } - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); + @Override + public void initialize() + { + this.entryWheel = Rotor.createRotor(0, 0, 0); + this.rotor1 = Rotor.createRotor(11, 0, 0); + this.rotor2 = Rotor.createRotor(12, 0, 0); + this.rotor3 = Rotor.createRotor(13, 0, 0); + this.reflector = new Reflector.ReflectorEnigmaDKD(); + } - addAvailableRotor(new Rotor.Rotor_K_D_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_K_D_II(0, 0)); - addAvailableRotor(new Rotor.Rotor_K_D_III(0, 0)); + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - addAvailableReflector(new Reflector.ReflectorEnigma_D_G31()); - } + @Override + public char encryptChar(char k) + { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + x = entryWheel.encryptForward(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); + x = rotor1.encryptForward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); + x = rotor1.encryptBackward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); + x = entryWheel.encryptBackward(x); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - @Override - public void initialize() - { - this.entryWheel = availableEntryWheels.get(0); - this.rotor1 = availableRotors.get(0); - this.rotor2 = availableRotors.get(1); - this.rotor3 = availableRotors.get(2); - this.reflector = availableReflectors.get(0); - } + @Override + public void setState(EnigmaStateBundle state) + { + this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); + this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = new Reflector.ReflectorEnigmaDKD(); + this.reflector.setConfiguration(state.getConfigurationReflector()); + this.reflector.setRotation(state.getRotationReflector()); + this.reflector.setRingSetting(state.getRingSettingReflector()); + } - @Override - public void nextState() - { - rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); - @Override - protected void generateState() { - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + state.setTypeRotor1(rotor1.getNumber()); + state.setTypeRotor2(rotor2.getNumber()); + state.setTypeRotor3(rotor3.getNumber()); - this.rotor1.setRotation(rot1).setRingSetting(ring1); - this.rotor2.setRotation(rot2).setRingSetting(ring2); - this.rotor3.setRotation(rot3).setRingSetting(ring3); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - this.reflector.setRotation(rotRef).setRingSetting(ringRef) - .setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); - } + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - @Override - public char encryptChar(char k) - { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + state.setTypeReflector(reflector.getNumber()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); + state.setConfigurationReflector(reflector.getConfiguration()); - @Override - public void setState(EnigmaStateBundle state) - { - this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); - this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = getReflector(state.getTypeReflector(), - state.getRotationReflector(), - state.getRingSettingReflector()) - .setConfiguration(state.getConfigurationReflector()); - } - - @Override - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); - - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); - state.setConfigurationReflector(reflector.getConfiguration()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch(protocol_version) - { - case 1: - int rot1 = getValue(s, 26); - s = removeDigit(s, 26); - int ring1 = getValue(s, 26); - s = removeDigit(s, 26); - int rot2 = getValue(s, 26); - s = removeDigit(s, 26); - int ring2 = getValue(s, 26); - s = removeDigit(s, 26); - int rot3 = getValue(s, 26); - s = removeDigit(s, 26); - int ring3 = getValue(s, 26); - s = removeDigit(s, 26); - int rotRef = getValue(s, 26); - s = removeDigit(s, 26); - int ringRef = getValue(s, 26); - s = removeDigit(s, 26); - - this.rotor1 = getRotor(0, rot1, ring1); - this.rotor2 = getRotor(1, rot2, ring2); - this.rotor3 = getRotor(2, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - this.reflector.setConfiguration(s); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - } - - @Override - public BigInteger getEncodedState(int protocol_version) { - BigInteger s = Plugboard.configurationToBigInteger(reflector.getConfiguration()); - s = addDigit(s, reflector.getRingSetting(), 26); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, 6, 20); //Machine #6 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - return s; - } + return state; + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java deleted file mode 100644 index 50492fe..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Implementation of the Enigma machine of name G31 (Abwehr) - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_G260 extends Enigma_G31 -{ - public Enigma_G260() - { - super(); - machineType = "G260"; - Log.d(MainActivity.APP_ID, "Created Enigma G260"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_G260_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_G260_II(0, 0)); - addAvailableRotor(new Rotor.Rotor_G260_III(0, 0)); - addAvailableReflector(new Reflector.Reflector_K_G260()); - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 5, 20); //Machine #5 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java deleted file mode 100644 index 4928f73..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java +++ /dev/null @@ -1,230 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Implementation of the Enigma machine of name G31 (Abwehr) - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_G31 extends Enigma -{ - private EntryWheel entryWheel; - Rotor rotor1; - Rotor rotor2; - Rotor rotor3; - Reflector reflector; - - public Enigma_G31() - { - super(); - machineType = "G31"; - Log.d(MainActivity.APP_ID, "Created Enigma G31"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_G31_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_G31_II(0, 0)); - addAvailableRotor(new Rotor.Rotor_G31_III(0, 0)); - addAvailableReflector(new Reflector.ReflectorEnigma_D_G31()); - } - - @Override - public void initialize() - { - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0); - this.rotor2 = getRotor(1); - this.rotor3 = getRotor(2); - this.reflector = getReflector(0); - } - - @Override - public void nextState() - { - rotor1.rotate(); - if (rotor1.isAtTurnoverPosition()) - { - rotor2.rotate(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - if(rotor3.isAtTurnoverPosition()) - { - reflector.setRotation(reflector.getRotation()+1); - } - } - } - } - - protected void generateState() - { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; - - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - } - - @Override - public char encryptChar(char k) { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - return (char) (x + 65); //Add Offset again, cast back to char and return - } - - @Override - public void setState(EnigmaStateBundle state) - { - this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); - this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = getReflector(state.getTypeReflector(), - state.getRotationReflector(), state.getRingSettingReflector()); - } - - @Override - public EnigmaStateBundle getState() { - EnigmaStateBundle state = new EnigmaStateBundle(); - - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r2 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r3 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - - int rot1 = getValue(s, 26); - s = removeDigit(s, 26); - int ring1 = getValue(s, 26); - s = removeDigit(s, 26); - int rot2 = getValue(s, 26); - s = removeDigit(s, 26); - int ring2 = getValue(s, 26); - s = removeDigit(s, 26); - int rot3 = getValue(s, 26); - s = removeDigit(s, 26); - int ring3 = getValue(s, 26); - s = removeDigit(s, 26); - int rotRef = getValue(s, 26); - s = removeDigit(s, 26); - int ringRef = getValue(s, 26); - - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 3, 20); //Machine #3 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - return s; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java deleted file mode 100644 index 93e0294..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Implementation of the Enigma machine of name G31 (Abwehr) - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_G312 extends Enigma_G31 -{ - public Enigma_G312() - { - super(); - machineType = "G312"; - Log.d(MainActivity.APP_ID, "Created Enigma G312"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_G312_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_G312_II(0, 0)); - addAvailableRotor(new Rotor.Rotor_G312_III(0, 0)); - addAvailableReflector(new Reflector.Reflector_G312()); - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 4, 20); //Machine #4 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java index 5117d63..2dc1d34 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_I.java @@ -1,239 +1,127 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Plugboard; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Concrete implementation of an enigma machine of name I + * Concrete implementation of an enigma machine of type I * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class Enigma_I extends Enigma { - EntryWheel entryWheel; - Rotor rotor1; - Rotor rotor2; - Rotor rotor3; - Reflector reflector; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; - Plugboard plugboard; + protected Reflector reflector; - public Enigma_I() - { - super(); - machineType = "I"; - Log.d(MainActivity.APP_ID, "Created Enigma I"); - } + protected Plugboard plugboard; - @Override - protected void establishAvailableParts() { - addAvailableEntryWheel(new EntryWheel.EntryWheel_ABCDEF()); - addAvailableRotor(new Rotor.Rotor_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_II(0,0)); - addAvailableRotor(new Rotor.Rotor_III(0,0)); - addAvailableRotor(new Rotor.Rotor_IV(0,0)); - addAvailableRotor(new Rotor.Rotor_V(0,0)); - addAvailableReflector(new Reflector.Reflector_A()); - addAvailableReflector(new Reflector.Reflector_B()); - addAvailableReflector(new Reflector.Reflector_C()); - } + public Enigma_I() + { + super(); + machineType = "I"; + } - @Override - public void initialize() - { - this.plugboard= new Plugboard(); - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0, 0, 0); - this.rotor2 = getRotor(1, 0, 0); - this.rotor3 = getRotor(2, 0, 0); - this.reflector = getReflector(0); - } + @Override + public void initialize() + { + this.plugboard= new Plugboard(); + this.rotor1 = Rotor.createRotor(1, 0, 0); + this.rotor2 = Rotor.createRotor(2, 0, 0); + this.rotor3 = Rotor.createRotor(3, 0, 0); + this.reflector = Reflector.createReflector(1); + } - @Override - public void nextState() - { - rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - protected void generateState() { - int r1, r2=-1, r3=-1; - r1 = rand.nextInt(5); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(5); - while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(5); - int ref = rand.nextInt(3); + @Override + public char encryptChar(char k) + { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + x = plugboard.encrypt(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); + x = rotor1.encryptForward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); + x = rotor1.encryptBackward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); + x = plugboard.encrypt(x); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); + @Override + public void setState(EnigmaStateBundle state) + { + plugboard.setConfiguration(state.getConfigurationPlugboard()); + rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + reflector = Reflector.createReflector(state.getTypeReflector()); + } - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(ref); + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); - this.plugboard = new Plugboard(); - plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); - } + state.setConfigurationPlugboard(plugboard.getConfiguration()); - @Override - public char encryptChar(char k) - { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = plugboard.encrypt(x); - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - x = plugboard.encrypt(x); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + state.setTypeRotor1(rotor1.getNumber()); + state.setTypeRotor2(rotor2.getNumber()); + state.setTypeRotor3(rotor3.getNumber()); - @Override - public void setState(EnigmaStateBundle state) - { - plugboard.setConfiguration(state.getConfigurationPlugboard()); - entryWheel = getEntryWheel(state.getTypeEntryWheel()); - rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - reflector = getReflector(state.getTypeReflector()); - } + state.setTypeReflector(reflector.getNumber()); - @Override - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - state.setConfigurationPlugboard(plugboard.getConfiguration()); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - - state.setTypeReflector(reflector.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r2 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r3 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int ref = getValue(s, availableReflectors.size()); - s = removeDigit(s, availableReflectors.size()); - int rot1 = getValue(s, 26); - s = removeDigit(s, 26); - int ring1 = getValue(s, 26); - s = removeDigit(s, 26); - int rot2 = getValue(s, 26); - s = removeDigit(s, 26); - int ring2 = getValue(s, 26); - s = removeDigit(s, 26); - int rot3 = getValue(s, 26); - s = removeDigit(s, 26); - int ring3 = getValue(s, 26); - s = removeDigit(s, 26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(ref); - - this.plugboard = new Plugboard(); - plugboard.setConfiguration(s); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - } - - @Override - public BigInteger getEncodedState(int protocol_version) { - BigInteger s = Plugboard.configurationToBigInteger(plugboard.getConfiguration()); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, reflector.getIndex(), availableReflectors.size()); - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 0, 20); //Machine #0 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } + return state; + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java index 1d0ca9c..214d699 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K.java @@ -1,227 +1,125 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of name K + * Implementation of the Enigma machine of type K (Switzerland) * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class Enigma_K extends Enigma { - private EntryWheel entryWheel; - Rotor rotor1; - Rotor rotor2; - Rotor rotor3; - Reflector reflector; + protected Rotor entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; - public Enigma_K() - { - super(); - machineType = "K"; - Log.d(MainActivity.APP_ID, "Created Enigma K"); - } + protected Reflector reflector; - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_K_D_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_K_D_II(0,0)); - addAvailableRotor(new Rotor.Rotor_K_D_III(0,0)); - addAvailableReflector(new Reflector.Reflector_K_G260()); - } + public Enigma_K() + { + super(); + machineType = "K"; + } + @Override + public void initialize() + { + this.entryWheel = Rotor.createRotor(0, 0, 0); + this.rotor1 = Rotor.createRotor(14, 0, 0); + this.rotor2 = Rotor.createRotor(15, 0, 0); + this.rotor3 = Rotor.createRotor(16, 0, 0); + this.reflector = Reflector.createReflector(7); + } - @Override - public void initialize() - { - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0, 0, 0); - this.rotor2 = getRotor(1, 0, 0); - this.rotor3 = getRotor(2, 0, 0); - this.reflector = getReflector(0); - } + @Override + public void nextState() + { + rotor1.rotate(); + if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + { + rotor2.rotate(); + this.doAnomaly = rotor2.doubleTurnAnomaly(); + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - @Override - public void nextState() - { - rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + @Override + public char encryptChar(char k) { + nextState(); + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + x = entryWheel.encryptForward(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); + x = rotor1.encryptForward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); + x = rotor1.encryptBackward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); + x = entryWheel.encryptBackward(x); + return (char) (x + 65); //Add Offset again, cast back to char and return + } - @Override - protected void generateState() { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; + @Override + public void setState(EnigmaStateBundle state) + { + this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); + this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = Reflector.createReflector(state.getTypeReflector()); + this.reflector.setRotation(state.getRotationReflector()); + this.reflector.setRingSetting(state.getRingSettingReflector()); + } - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + @Override + public EnigmaStateBundle getState() { + EnigmaStateBundle state = new EnigmaStateBundle(); - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - } + state.setTypeRotor1(rotor1.getNumber()); + state.setTypeRotor2(rotor2.getNumber()); + state.setTypeRotor3(rotor3.getNumber()); - @Override - public char encryptChar(char k) { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - return (char) (x + 65); //Add Offset again, cast back to char and return - } + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); - @Override - public void setState(EnigmaStateBundle state) - { - this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); - this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = getReflector(state.getTypeReflector(), state.getRotationReflector(), state.getRingSettingReflector()); - } + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); - @Override - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); + state.setTypeReflector(reflector.getNumber()); + state.setRotationReflector(reflector.getRotation()); + state.setRingSettingReflector(reflector.getRingSetting()); - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r2 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r3 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - - int rot1 = getValue(s,26); - s = removeDigit(s,26); - int ring1 = getValue(s,26); - s = removeDigit(s,26); - int rot2 = getValue(s,26); - s = removeDigit(s,26); - int ring2 = getValue(s,26); - s = removeDigit(s,26); - int rot3 = getValue(s,26); - s = removeDigit(s,26); - int ring3 = getValue(s,26); - s = removeDigit(s,26); - int rotRef = getValue(s,26); - s = removeDigit(s,26); - int ringRef = getValue(s,26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(),26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(),26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 7, 20); //Machine #7 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } + return state; + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java deleted file mode 100644 index c751069..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java +++ /dev/null @@ -1,240 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Plugboard; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Concrete implementation of an enigma machine of name D - * This machine has a rewirable UKW, non changeable rotors. - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_KD extends Enigma { - - private EntryWheel entryWheel; - private Rotor rotor1; - private Rotor rotor2; - private Rotor rotor3; - private Reflector reflector; - - public Enigma_KD() - { - super(); - machineType = "KD"; - Log.d(MainActivity.APP_ID, "Created Enigma KD"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - - addAvailableRotor(new Rotor.Rotor_KD_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_KD_II(0, 0)); - addAvailableRotor(new Rotor.Rotor_KD_III(0, 0)); - - addAvailableReflector(new Reflector.ReflectorEnigma_KD()); - } - - @Override - public void initialize() - { - this.entryWheel = availableEntryWheels.get(0); - this.rotor1 = availableRotors.get(0); - this.rotor2 = availableRotors.get(1); - this.rotor3 = availableRotors.get(2); - this.reflector = availableReflectors.get(0); - } - - @Override - public void nextState() - { - rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } - - @Override - protected void generateState() { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; - - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - this.reflector.setRotation(rotRef).setRingSetting(ringRef) - .setConfiguration(Plugboard.seedToReflectorConfiguration(rand)); - } - - @Override - public char encryptChar(char k) - { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - return (char) (x + 65); //Add Offset again, cast back to char and return - } - - @Override - public void setState(EnigmaStateBundle state) - { - this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); - this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = getReflector(state.getTypeReflector(), - state.getRotationReflector(), - state.getRingSettingReflector()) - .setConfiguration(state.getConfigurationReflector()); - } - - @Override - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); - - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); - state.setConfigurationReflector(reflector.getConfiguration()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r2 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r3 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - - int rot1 = getValue(s, 26); - s = removeDigit(s, 26); - int ring1 = getValue(s, 26); - s = removeDigit(s, 26); - int rot2 = getValue(s, 26); - s = removeDigit(s, 26); - int ring2 = getValue(s, 26); - s = removeDigit(s, 26); - int rot3 = getValue(s, 26); - s = removeDigit(s, 26); - int ring3 = getValue(s, 26); - s = removeDigit(s, 26); - int rotRef = getValue(s, 26); - s = removeDigit(s, 26); - int ringRef = getValue(s, 26); - s = removeDigit(s, 26); - - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - this.reflector.setConfiguration(s); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - - } - - @Override - public BigInteger getEncodedState(int protocol_version) { - BigInteger s = Plugboard.configurationToBigInteger(reflector.getConfiguration()); - s = addDigit(s, reflector.getRingSetting(), 26); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 12, 20); //Machine #12 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java deleted file mode 100644 index 830999b..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Airforce.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Implementation of the Enigma machine of name K (Switzerland, Airforce) - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_K_Swiss_Airforce extends Enigma_K -{ - public Enigma_K_Swiss_Airforce() - { - super(); - machineType = "KSA"; - Log.d(MainActivity.APP_ID, "Created Enigma KSA"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_K_Swiss_Airforce_I(0,0)); - addAvailableRotor(new Rotor.Rotor_K_Swiss_Airforce_II(0,0)); - addAvailableRotor(new Rotor.Rotor_K_Swiss_Airforce_III(0,0)); - addAvailableReflector(new Reflector.Reflector_K_G260()); - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(),26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(),26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 9, 20); //Machine #9 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java deleted file mode 100644 index 6707c15..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_K_Swiss_Standard.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Implementation of the Enigma machine of name K (Switzerland) - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_K_Swiss_Standard extends Enigma_K -{ - public Enigma_K_Swiss_Standard() - { - super(); - machineType = "KS"; - Log.d(MainActivity.APP_ID, "Created Enigma KS"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_KSwiss_Standard_I(0,0)); - addAvailableRotor(new Rotor.Rotor_KSwiss_Standard_II(0,0)); - addAvailableRotor(new Rotor.Rotor_KSwiss_Standard_III(0,0)); - addAvailableReflector(new Reflector.Reflector_K_G260()); - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(),26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(),26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 8, 20); //Machine #8 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } - -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java index ed07d42..6af47a4 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M3.java @@ -1,99 +1,40 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Plugboard; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** * Concrete implementation of an enigma machine model M3 * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class Enigma_M3 extends Enigma_I { - public Enigma_M3() - { - super(); - machineType = "M3"; - Log.d(MainActivity.APP_ID, "Created Enigma M3"); - } - - @Override - protected void establishAvailableParts() - { - addAvailableEntryWheel(new EntryWheel.EntryWheel_ABCDEF()); - addAvailableRotor(new Rotor.Rotor_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_II(0,0)); - addAvailableRotor(new Rotor.Rotor_III(0,0)); - addAvailableRotor(new Rotor.Rotor_IV(0,0)); - addAvailableRotor(new Rotor.Rotor_V(0,0)); - addAvailableRotor(new Rotor.Rotor_VI(0,0)); - addAvailableRotor(new Rotor.Rotor_VII(0,0)); - addAvailableRotor(new Rotor.Rotor_VIII(0,0)); - addAvailableReflector(new Reflector.Reflector_B()); - addAvailableReflector(new Reflector.Reflector_C()); - } - - @Override - protected void generateState() { - int r1, r2=-1, r3=-1; - r1 = rand.nextInt(8); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); - while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); - int ref = rand.nextInt(2); - - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(ref); - - this.plugboard = new Plugboard(); - plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); - } - - @Override - public BigInteger getEncodedState(int protocol_version) { - BigInteger s = Plugboard.configurationToBigInteger(plugboard.getConfiguration()); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - s = addDigit(s, reflector.getIndex(), availableReflectors.size()); - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - s = addDigit(s, 1, 20); //Machine #1 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } + public Enigma_M3() + { + machineType = "M3"; + } + @Override + public void initialize() + { + this.plugboard = new Plugboard(); + this.rotor1 = Rotor.createRotor(1, 0, 0); + this.rotor2 = Rotor.createRotor(2, 0, 0); + this.rotor3 = Rotor.createRotor(3, 0, 0); + this.reflector = Reflector.createReflector(2); + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java index 29e160a..6d03c0d 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_M4.java @@ -1,320 +1,155 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; -import android.util.Log; - -import java.math.BigInteger; -import java.util.ArrayList; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Plugboard; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Concrete Implementation of the Enigma Machine name M4 of the german Kriegsmarine + * Concrete Implementation of the Enigma Machine type M4 of the german Kriegsmarine * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class Enigma_M4 extends Enigma { - private ArrayList availableThinRotors; - private EntryWheel entryWheel; - private Rotor rotor1; - private Rotor rotor2; - private Rotor rotor3; + private Rotor rotor1; + private Rotor rotor2; + private Rotor rotor3; + private Rotor rotor4; - private Rotor rotor4; + private Reflector reflector; - private Reflector reflector; + private Plugboard plugboard; - private Plugboard plugboard; + public Enigma_M4() + { + super(); + machineType = "M4"; + } - public Enigma_M4() - { - super(); - machineType = "M4"; - Log.d(MainActivity.APP_ID, "Created Enigma M4"); - } + @Override + public void initialize() + { + this.plugboard = new Plugboard(); + this.rotor1 = Rotor.createRotor(1, 0, 0); + this.rotor2 = Rotor.createRotor(2, 0, 0); + this.rotor3 = Rotor.createRotor(3, 0, 0); + this.rotor4 = Rotor.createRotor(9, 0, 0); + this.reflector = Reflector.createReflector(4); + this.prefAnomaly = true; + } - private void addAvailableThinRotor(Rotor r) - { - if(availableThinRotors == null) availableThinRotors = new ArrayList<>(); - availableThinRotors.add(availableThinRotors.size(), r.setIndex(availableThinRotors.size())); - } + @Override + /** + * Set the enigma into the next mechanical state. + * This rotates the first rotor and eventually also the second/third. + * Also this method handles the anomaly in case it should happen. + */ + public void nextState() + { + //Rotate rotors + rotor1.rotate(); + //Eventually turn next rotor (usual turnOver or anomaly) + if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) + { + rotor2.rotate(); + //Set doAnomaly for next call of encryptChar + this.doAnomaly = rotor2.doubleTurnAnomaly(); + //Eventually rotate next rotor + if (rotor2.isAtTurnoverPosition()) + { + rotor3.rotate(); + } + } + } - private Rotor getThinRotor(int index) - { - if(availableThinRotors == null || availableThinRotors.size() == 0) return null; - return availableThinRotors.get(index % availableThinRotors.size()).getInstance(); - } + @Override + /** + * Substitute char k by sending the signal through the enigma. + * The signal passes the plugboard, the rotors and returns back after going through the + * reflector wheel. + * + * @param k input char + * @return substituted output char + */ + public char encryptChar(char k) + { + nextState(); //Rotate rotors + int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) + //Encryption + //forward direction + x = plugboard.encrypt(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); + x = rotor1.encryptForward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); + x = rotor2.encryptForward(x); + x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); + x = rotor3.encryptForward(x); + x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + rotor4.getRotation() - rotor4.getRingSetting()); + x = rotor4.encryptForward(x); + x = rotor1.normalize(x - rotor4.getRotation() + rotor4.getRingSetting()); + //backward direction + x = reflector.encrypt(x); + x = rotor1.normalize(x + rotor4.getRotation() - rotor4.getRingSetting()); + x = rotor4.encryptBackward(x); + x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - rotor4.getRotation() + rotor4.getRingSetting()); + x = rotor3.encryptBackward(x); + x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); + x = rotor2.encryptBackward(x); + x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); + x = rotor1.encryptBackward(x); + x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); + x = plugboard.encrypt(x); + return (char) (x + 65); //Add Offset again and cast back to char + } - private Rotor getThinRotor(int index, int rotation, int ringSettings) - { - Rotor r = getThinRotor(index); - if(r == null) return null; - return r.setRotation(rotation).setRingSetting(ringSettings); - } + @Override + public void setState(EnigmaStateBundle state) + { + rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + rotor4 = Rotor.createRotor(state.getTypeRotor4(), state.getRotationRotor4(), state.getRingSettingRotor4()); + reflector = Reflector.createReflector(state.getTypeReflector()); + plugboard.setConfiguration(state.getConfigurationPlugboard()); - @Override - protected void establishAvailableParts() - { - Log.d(MainActivity.APP_ID, "Established"); - addAvailableEntryWheel(new EntryWheel.EntryWheel_ABCDEF()); - addAvailableRotor(new Rotor.Rotor_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_II(0,0)); - addAvailableRotor(new Rotor.Rotor_III(0,0)); - addAvailableRotor(new Rotor.Rotor_IV(0,0)); - addAvailableRotor(new Rotor.Rotor_V(0,0)); - addAvailableRotor(new Rotor.Rotor_VI(0,0)); - addAvailableRotor(new Rotor.Rotor_VII(0, 0)); - addAvailableRotor(new Rotor.Rotor_VIII(0,0)); - addAvailableThinRotor(new Rotor.Rotor_M4_Beta(0, 0)); - addAvailableThinRotor(new Rotor.Rotor_M4_Gamma(0, 0)); - addAvailableReflector(new Reflector.Reflector_Thin_B()); - addAvailableReflector(new Reflector.ReflectorThinC()); - } + } - @Override - public void initialize() - { - Log.d(MainActivity.APP_ID, "Initialized"); - this.plugboard = new Plugboard(); - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0, 0, 0); - this.rotor2 = getRotor(1, 0, 0); - this.rotor3 = getRotor(2, 0, 0); - this.rotor4 = getThinRotor(0, 0, 0); - this.reflector = getReflector(0); - } + @Override + public EnigmaStateBundle getState() + { + EnigmaStateBundle state = new EnigmaStateBundle(); + state.setTypeRotor1(rotor1.getNumber()); + state.setTypeRotor2(rotor2.getNumber()); + state.setTypeRotor3(rotor3.getNumber()); + state.setTypeRotor4(rotor4.getNumber()); - @Override - /** - * Set the enigma into the next mechanical state. - * This rotates the first rotor and eventually also the second/third. - * Also this method handles the anomaly in case it should happen. - */ - public void nextState() - { - //Rotate rotors - rotor1.rotate(); - //Eventually turn next rotor (usual turnOver or anomaly) - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - //Set doAnomaly for next call of encryptChar - this.doAnomaly = rotor2.doubleTurnAnomaly(); - //Eventually rotate next rotor - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } + state.setRotationRotor1(rotor1.getRotation()); + state.setRotationRotor2(rotor2.getRotation()); + state.setRotationRotor3(rotor3.getRotation()); + state.setRotationRotor4(rotor4.getRotation()); - @Override - protected void generateState() { - int r1, r2=-1, r3=-1; - int r4; - int ref; - r1 = rand.nextInt(8); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); - while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); - r4 = rand.nextInt(2); - ref = rand.nextInt(2); + state.setRingSettingRotor1(rotor1.getRingSetting()); + state.setRingSettingRotor2(rotor2.getRingSetting()); + state.setRingSettingRotor3(rotor3.getRingSetting()); + state.setRingSettingRotor4(rotor4.getRingSetting()); - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rot4 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ring4 = rand.nextInt(26); - int ringRef = rand.nextInt(26); + state.setTypeReflector(reflector.getNumber()); - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.rotor4 = getThinRotor(r4, rot4, ring4); + state.setConfigurationPlugboard(plugboard.getConfiguration()); - this.reflector = getReflector(ref, rotRef, ringRef); - - this.plugboard = new Plugboard(); - this.plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand)); - } - - @Override - /** - * Substitute char k by sending the signal through the enigma. - * The signal passes the plugboard, the rotors and returns back after going through the - * reflector wheel. - * - * @param k input char - * @return substituted output char - */ - public char encryptChar(char k) - { - nextState(); //Rotate rotors - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = plugboard.encrypt(x); - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + rotor4.getRotation() - rotor4.getRingSetting()); - x = rotor4.encryptForward(x); - x = rotor1.normalize(x - rotor4.getRotation() + rotor4.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor4.getRotation() - rotor4.getRingSetting()); - x = rotor4.encryptBackward(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - rotor4.getRotation() + rotor4.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - x = plugboard.encrypt(x); - return (char) (x + 65); //Add Offset again and cast back to char - } - - @Override - public void setState(EnigmaStateBundle state) - { - rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - rotor4 = getThinRotor(state.getTypeRotor4(), state.getRotationRotor4(), state.getRingSettingRotor4()); - reflector = getReflector(state.getTypeReflector()); - plugboard.setConfiguration(state.getConfigurationPlugboard()); - - } - - @Override - public EnigmaStateBundle getState() - { - EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - state.setTypeRotor4(rotor4.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - state.setRotationRotor4(rotor4.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setRingSettingRotor4(rotor4.getRingSetting()); - - state.setTypeReflector(reflector.getIndex()); - - state.setConfigurationPlugboard(plugboard.getConfiguration()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s, availableRotors.size()); - s = removeDigit(s, availableRotors.size()); - int r2 = getValue(s, availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r3 = getValue(s, availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r4 = getValue(s, availableThinRotors.size()); - s = removeDigit(s,availableThinRotors.size()); - int ref = getValue(s, availableReflectors.size()); - s = removeDigit(s,availableReflectors.size()); - - int rot1 = getValue(s, 26); - s = removeDigit(s,26); - int ring1 = getValue(s, 26); - s = removeDigit(s,26); - int rot2 = getValue(s, 26); - s = removeDigit(s,26); - int ring2 = getValue(s, 26); - s = removeDigit(s,26); - int rot3 = getValue(s, 26); - s = removeDigit(s,26); - int ring3 = getValue(s, 26); - s = removeDigit(s,26); - int rot4 = getValue(s, 26); - s = removeDigit(s,26); - int ring4 = getValue(s, 26); - s = removeDigit(s,26); - int rotRef = getValue(s, 26); - s = removeDigit(s,26); - int ringRef = getValue(s, 26); - s = removeDigit(s, 26); - - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.rotor4 = getThinRotor(r4, rot4, ring4); - this.reflector = getReflector(ref, rotRef, ringRef); - this.plugboard = new Plugboard(); - plugboard.setConfiguration(s); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - } - - @Override - public BigInteger getEncodedState(int protocol_version) { - BigInteger s = Plugboard.configurationToBigInteger(plugboard.getConfiguration()); - s = addDigit(s, reflector.getRingSetting(), 26); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor4.getRingSetting(), 26); - s = addDigit(s, rotor4.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(), 26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(), 26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, reflector.getIndex(), availableReflectors.size()); - s = addDigit(s, rotor4.getIndex(), availableThinRotors.size()); - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 2, 20); - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } + return state; + } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java deleted file mode 100644 index 028fce2..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java +++ /dev/null @@ -1,231 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma; - -import android.util.Log; - -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; - -/** - * Implementation of the Enigma machine of name R ("Rocket", Reichsbahn) - * Copyright (C) 2015 Paul Schaub - */ -public class Enigma_R extends Enigma -{ - private EntryWheel entryWheel; - private Rotor rotor1; - private Rotor rotor2; - private Rotor rotor3; - - private Reflector reflector; - - public Enigma_R() - { - super(); - machineType = "R"; - Log.d(MainActivity.APP_ID, "Created Enigma R"); - } - - @Override - protected void establishAvailableParts() { - addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ()); - addAvailableRotor(new Rotor.Rotor_R_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_R_II(0,0)); - addAvailableRotor(new Rotor.Rotor_R_III(0,0)); - addAvailableReflector(new Reflector.Reflector_R()); - - } - - @Override - public void initialize() - { - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0); - this.rotor2 = getRotor(1); - this.rotor3 = getRotor(2); - this.reflector = getReflector(0); - } - - @Override - public void nextState() - { - rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) - { - rotor2.rotate(); - this.doAnomaly = rotor2.doubleTurnAnomaly(); - if (rotor2.isAtTurnoverPosition()) - { - rotor3.rotate(); - } - } - } - - @Override - protected void generateState() - { - int r1, r2=-1, r3; - r1 = rand.nextInt(3); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(3); - r3 = 3 - r1 - r2; - - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - } - - @Override - public char encryptChar(char k) { - nextState(); - int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) - //Encryption - //forward direction - x = entryWheel.encryptForward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); - x = rotor1.encryptForward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); - x = rotor2.encryptForward(x); - x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); - x = rotor3.encryptForward(x); - x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); - //backward direction - x = reflector.encrypt(x); - x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); - x = rotor3.encryptBackward(x); - x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); - x = rotor2.encryptBackward(x); - x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); - x = rotor1.encryptBackward(x); - x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); - x = entryWheel.encryptBackward(x); - return (char) (x + 65); //Add Offset again, cast back to char and return - } - - @Override - public void setState(EnigmaStateBundle state) - { - this.entryWheel = getEntryWheel(state.getTypeEntryWheel()); - this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = getReflector(state.getTypeReflector(), state.getRotationReflector(), state.getRingSettingReflector()); - } - - @Override - public EnigmaStateBundle getState() { - EnigmaStateBundle state = new EnigmaStateBundle(); - - state.setTypeEntryWheel(entryWheel.getIndex()); - - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); - - state.setRotationRotor1(rotor1.getRotation()); - state.setRotationRotor2(rotor2.getRotation()); - state.setRotationRotor3(rotor3.getRotation()); - - state.setRingSettingRotor1(rotor1.getRingSetting()); - state.setRingSettingRotor2(rotor2.getRingSetting()); - state.setRingSettingRotor3(rotor3.getRingSetting()); - - state.setTypeReflector(reflector.getIndex()); - state.setRotationReflector(reflector.getRotation()); - state.setRingSettingReflector(reflector.getRingSetting()); - - return state; - } - - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r2 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r3 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - - int rot1 = getValue(s, 26); - s = removeDigit(s, 26); - int ring1 = getValue(s, 26); - s = removeDigit(s, 26); - int rot2 = getValue(s, 26); - s = removeDigit(s, 26); - int ring2 = getValue(s, 26); - s = removeDigit(s, 26); - int rot3 = getValue(s, 26); - s = removeDigit(s, 26); - int ring3 = getValue(s, 26); - s = removeDigit(s, 26); - int rotRef = getValue(s, 26); - s = removeDigit(s, 26); - int ringRef = getValue(s, 26); - s = removeDigit(s, 26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - - s = addDigit(s, rotor3.getRingSetting(),26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(),26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 10, 20); //Machine #10 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java index aedc432..1c4ee54 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_T.java @@ -1,75 +1,57 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma; import android.util.Log; -import java.math.BigInteger; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.parts.EntryWheel; -import de.vanitasvitae.enigmandroid.enigma.parts.Reflector; -import de.vanitasvitae.enigmandroid.enigma.parts.Rotor; +import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector; +import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor; /** - * Implementation of the Enigma machine of name T Tirpitz + * Implementation of the Enigma machine of type T Tirpitz * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class Enigma_T extends Enigma { - private EntryWheel entryWheel; - private Rotor rotor1; - private Rotor rotor2; - private Rotor rotor3; - private Reflector reflector; + protected Rotor entryWheel; + protected Rotor rotor1; + protected Rotor rotor2; + protected Rotor rotor3; + protected Reflector reflector; public Enigma_T() { super(); - machineType = "T"; - Log.d(MainActivity.APP_ID, "Created Enigma T"); - } - - @Override - protected void establishAvailableParts() { - addAvailableEntryWheel(new EntryWheel.EntryWheel_T()); - addAvailableRotor(new Rotor.Rotor_T_I(0, 0)); - addAvailableRotor(new Rotor.Rotor_T_II(0,0)); - addAvailableRotor(new Rotor.Rotor_T_III(0,0)); - addAvailableRotor(new Rotor.Rotor_T_IV(0,0)); - addAvailableRotor(new Rotor.Rotor_T_V(0,0)); - addAvailableRotor(new Rotor.Rotor_T_VI(0,0)); - addAvailableRotor(new Rotor.Rotor_T_VII(0,0)); - addAvailableRotor(new Rotor.Rotor_T_VIII(0,0)); - addAvailableReflector(new Reflector.ReflectorEnigma_T()); + machineType = "K"; } @Override public void initialize() { - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(0); - this.rotor2 = getRotor(1); - this.rotor3 = getRotor(2); - this.reflector = getReflector(0); + this.entryWheel = Rotor.createRotor(17,0,0); + this.rotor1 = Rotor.createRotor(18, 0, 0); + this.rotor2 = Rotor.createRotor(19, 0, 0); + this.rotor3 = Rotor.createRotor(20, 0, 0); + this.reflector = Reflector.createReflector(8); } @Override public void nextState() { rotor1.rotate(); - if (rotor1.isAtTurnoverPosition() || this.doAnomaly) + if (rotor1.isAtTurnoverPosition() || (this.doAnomaly && prefAnomaly)) { rotor2.rotate(); this.doAnomaly = rotor2.doubleTurnAnomaly(); @@ -80,63 +62,53 @@ public class Enigma_T extends Enigma } } - @Override - protected void generateState() { - int r1, r2=-1, r3=-1; - r1 = rand.nextInt(8); - while(r2 == -1 || r2 == r1) r2 = rand.nextInt(8); - while(r3 == -1 || r3 == r2 || r3 == r1) r3 = rand.nextInt(8); - - int rot1 = rand.nextInt(26); - int rot2 = rand.nextInt(26); - int rot3 = rand.nextInt(26); - int rotRef = rand.nextInt(26); - int ring1 = rand.nextInt(26); - int ring2 = rand.nextInt(26); - int ring3 = rand.nextInt(26); - int ringRef = rand.nextInt(26); - - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - } - @Override public char encryptChar(char k) { nextState(); int x = ((int) k)-65; //Cast to int and remove Unicode Offset (A=65 in Unicode.) //Encryption //forward direction + String log = " in: " + (char) (x+65); x = entryWheel.encryptForward(x); + log = log + " ew: " + (char) (x+65); x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting()); x = rotor1.encryptForward(x); + log = log + " r1: " + (char) (x+65); x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting() + rotor2.getRotation() - rotor2.getRingSetting()); x = rotor2.encryptForward(x); + log = log + " r2: " + (char) (x+65); x = rotor1.normalize(x - rotor2.getRotation() + rotor2.getRingSetting() + rotor3.getRotation() - rotor3.getRingSetting()); x = rotor3.encryptForward(x); + log = log + " r3: " + (char) (x+65); x = rotor1.normalize(x - rotor3.getRotation() + rotor3.getRingSetting() + reflector.getRotation() - reflector.getRingSetting()); //backward direction x = reflector.encrypt(x); + log = log + " ref: " + (char) (x+65); x = rotor1.normalize(x + rotor3.getRotation() - rotor3.getRingSetting() - reflector.getRotation() + reflector.getRingSetting()); x = rotor3.encryptBackward(x); + log = log + " r3: " + (char) (x+65); x = rotor1.normalize(x + rotor2.getRotation() - rotor2.getRingSetting() - rotor3.getRotation() + rotor3.getRingSetting()); x = rotor2.encryptBackward(x); + log = log + " r2: " + (char) (x+65); x = rotor1.normalize(x + rotor1.getRotation() - rotor1.getRingSetting() - rotor2.getRotation() + rotor2.getRingSetting()); x = rotor1.encryptBackward(x); + log = log + " r1: " + (char) (x+65); x = rotor1.normalize(x - rotor1.getRotation() + rotor1.getRingSetting()); x = entryWheel.encryptBackward(x); + log = log + " ew/out: " + (char) (x+65); + Log.d("EnryptionLog",log); return (char) (x + 65); //Add Offset again, cast back to char and return } @Override public void setState(EnigmaStateBundle state) { - this.entryWheel = getEntryWheel(0); - this.rotor1 = getRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); - this.rotor2 = getRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); - this.rotor3 = getRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); - this.reflector = getReflector(state.getTypeReflector(), state.getRotationReflector(), state.getRingSettingReflector()); + this.entryWheel = Rotor.createRotor(state.getTypeEntryWheel(), 0, 0); + this.rotor1 = Rotor.createRotor(state.getTypeRotor1(), state.getRotationRotor1(), state.getRingSettingRotor1()); + this.rotor2 = Rotor.createRotor(state.getTypeRotor2(), state.getRotationRotor2(), state.getRingSettingRotor2()); + this.rotor3 = Rotor.createRotor(state.getTypeRotor3(), state.getRotationRotor3(), state.getRingSettingRotor3()); + this.reflector = Reflector.createReflector(state.getTypeReflector()); + this.reflector.setRotation(state.getRotationReflector()); + this.reflector.setRingSetting(state.getRingSettingReflector()); } @Override @@ -144,9 +116,9 @@ public class Enigma_T extends Enigma { EnigmaStateBundle state = new EnigmaStateBundle(); - state.setTypeRotor1(rotor1.getIndex()); - state.setTypeRotor2(rotor2.getIndex()); - state.setTypeRotor3(rotor3.getIndex()); + state.setTypeRotor1(rotor1.getNumber()); + state.setTypeRotor2(rotor2.getNumber()); + state.setTypeRotor3(rotor3.getNumber()); state.setRotationRotor1(rotor1.getRotation()); state.setRotationRotor2(rotor2.getRotation()); @@ -156,73 +128,13 @@ public class Enigma_T extends Enigma state.setRingSettingRotor2(rotor2.getRingSetting()); state.setRingSettingRotor3(rotor3.getRingSetting()); - state.setTypeReflector(reflector.getIndex()); + state.setTypeReflector(reflector.getNumber()); state.setRotationReflector(reflector.getRotation()); state.setRingSettingReflector(reflector.getRingSetting()); - state.setTypeEntryWheel(entryWheel.getIndex()); + state.setTypeEntryWheel(entryWheel.getNumber()); return state; } - @Override - public void restoreState(BigInteger s, int protocol_version) - { - switch (protocol_version) - { - case 1: - int r1 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r2 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int r3 = getValue(s,availableRotors.size()); - s = removeDigit(s,availableRotors.size()); - int rot1 = getValue(s,26); - s = removeDigit(s,26); - int ring1 = getValue(s,26); - s = removeDigit(s,26); - int rot2 = getValue(s,26); - s = removeDigit(s,26); - int ring2 = getValue(s,26); - s = removeDigit(s,26); - int rot3 = getValue(s,26); - s = removeDigit(s,26); - int ring3 = getValue(s,26); - s = removeDigit(s,26); - int rotRef = getValue(s,26); - s = removeDigit(s, 26); - int ringRef = getValue(s,26); - - this.rotor1 = getRotor(r1, rot1, ring1); - this.rotor2 = getRotor(r2, rot2, ring2); - this.rotor3 = getRotor(r3, rot3, ring3); - this.reflector = getReflector(0, rotRef, ringRef); - break; - - default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version); - } - - } - - @Override - public BigInteger getEncodedState(int protocol_version) - { - BigInteger s = BigInteger.valueOf(reflector.getRingSetting()); - s = addDigit(s, reflector.getRotation(), 26); - s = addDigit(s, rotor3.getRingSetting(),26); - s = addDigit(s, rotor3.getRotation(), 26); - s = addDigit(s, rotor2.getRingSetting(),26); - s = addDigit(s, rotor2.getRotation(), 26); - s = addDigit(s, rotor1.getRingSetting(), 26); - s = addDigit(s, rotor1.getRotation(), 26); - - s = addDigit(s, rotor3.getIndex(), availableRotors.size()); - s = addDigit(s, rotor2.getIndex(), availableRotors.size()); - s = addDigit(s, rotor1.getIndex(), availableRotors.size()); - - s = addDigit(s, 11, 20); //Machine #11 - s = addDigit(s, protocol_version, MainActivity.max_protocol_version); - - return s; - } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java new file mode 100644 index 0000000..affdc50 --- /dev/null +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java @@ -0,0 +1,56 @@ +package de.vanitasvitae.enigmandroid.enigma; + +/** + * Plugboard of the enigma + * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae + */ +public class Plugboard +{ + public static final int[] empty = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; + private int[] plugs; + + public Plugboard() + { + plugs = empty; + } + + public Plugboard(int[] conf) + { + this.plugs = conf; + } + + public void setConfiguration(int[] conf) + { + this.plugs = conf; + } + + public int[] getConfiguration() + { + return this.plugs; + } + + /** + * Encrypt input via plugboard connections + * @param input input symbol to encryptString + * @return encrypted symbol + */ + public int encrypt(int input) + { + return plugs[(input+plugs.length)%plugs.length]; + } +} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java index 9b634b2..518ecfc 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java @@ -1,17 +1,3 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.enigma.inputPreparer; import android.widget.EditText; @@ -22,10 +8,10 @@ import android.widget.EditText; */ public abstract class EditTextAdapter { - EditText editText; - String content; + protected EditText editText; + protected String content; - EditTextAdapter(EditText editText) + public EditTextAdapter(EditText editText) { this.editText = editText; } @@ -68,9 +54,7 @@ public abstract class EditTextAdapter { switch (type) { - case "4": return new EditTextAdapterGap(editText, 4); - case "5": return new EditTextAdapterGap(editText, 5); - case "6": return new EditTextAdapterGap(editText, 6); + case "5": return new EditTextAdapter5Gap(editText); case "no": return new EditTextAdapterNoGap(editText); default: return new EditTextAdapterNoGap(editText); } @@ -91,13 +75,11 @@ public abstract class EditTextAdapter } } - public static class EditTextAdapterGap extends EditTextAdapter + public static class EditTextAdapter5Gap extends EditTextAdapter { - int blockSize; - public EditTextAdapterGap(EditText editText, int blockSize) + public EditTextAdapter5Gap(EditText editText) { super(editText); - this.blockSize = blockSize; } @Override @@ -106,14 +88,13 @@ public abstract class EditTextAdapter this.content = text; String out = ""; int i; - for(i=0; i. - */ package de.vanitasvitae.enigmandroid.enigma.inputPreparer; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.SettingsActivity; - /** * Preparer class that prepares input text to only consist of [A..Z] * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ -public abstract class InputPreparer { - InputPreparer child; - - public String prepareString(String in) { - if (child != null) - return child.prepareString(this.prepare(in)); - else - return prepare(in); - } - - protected abstract String prepare(String input); - - public static InputPreparer createInputPreparer() +public abstract class InputPreparer +{ + /** + * Prepare the input String in a way that it only contains letters from [A..Z]. + * Replace special characters, remove spaces and spell numbers. + * @param input String + * @return prepared String + */ + public String prepareString(String input) { - MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(main); + input = input.toUpperCase(); + input = input.replace("Ä","AE").replace("Ö","OE").replace("Ü","UE").replace("ß","SS"); + String output = ""; - boolean replaceSpecialChars = sharedPreferences.getBoolean(SettingsActivity.PREF_REPLACE_SPECIAL_CHARACTERS, true); - String num_lang = sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, main.getResources(). - getStringArray(R.array.pref_alias_numeric_spelling_language)[0]); - InputPreparer inPrep = new RemoveIllegalCharacters(); - if(replaceSpecialChars) inPrep = new ReplaceSpecialCharacters(inPrep); - switch (num_lang) + for (char x : input.toCharArray()) { - case "de": inPrep = new ReplaceNumbersGerman(inPrep); - break; - case "en": inPrep = new ReplaceNumbersEnglish(inPrep); - break; - case "fr": inPrep = new ReplaceNumbersFrench(inPrep); - break; - case "sp": inPrep = new ReplaceNumbersSpanish(inPrep); - break; - case "it": inPrep = new ReplaceNumbersItalian(inPrep); - break; - default: - break; - } - return inPrep; - } - - public static class ReplaceSpecialCharacters extends InputPreparer { -// --Commented out by Inspection START (07.11.15 19:47): -// public ReplaceSpecialCharacters() { -// this.child = null; -// } -// --Commented out by Inspection STOP (07.11.15 19:47) - - public ReplaceSpecialCharacters(InputPreparer child) { - this.child = child; - } - - protected String prepare(String input) { - input = input.toUpperCase(); - return input.replace("Ä", "AE").replace("Ö", "OE").replace("Ü", "UE").replace("ß", "SS").replace(" ",""); - } - } - - /** - * Concrete implementation of a german InputPreparer - */ - public static class ReplaceNumbersGerman extends InputPreparer { -// --Commented out by Inspection START (07.11.15 19:47): -// public ReplaceNumbersGerman() { -// this.child = null; -// } -// --Commented out by Inspection STOP (07.11.15 19:47) - - public ReplaceNumbersGerman(InputPreparer child) { - this.child = child; - } - - protected String prepare(String input) { - input = input.replace("0", "NULL") - .replace("1", "EINS") - .replace("2", "ZWEI") - .replace("3", "DREI") - .replace("4", "VIER") - .replace("5", "FUENF") - .replace("6", "SECHS") - .replace("7", "SIEBEN") - .replace("8", "ACHT") - .replace("9", "NEUN"); - return input; - } - } - - /** - * Concrete implementation of a french InputPreparer - */ - public static class ReplaceNumbersFrench extends InputPreparer { -// --Commented out by Inspection START (07.11.15 19:47): -// public ReplaceNumbersFrench() { -// this.child = null; -// } -// --Commented out by Inspection STOP (07.11.15 19:47) - - public ReplaceNumbersFrench(InputPreparer child) { - this.child = child; - } - - protected String prepare(String input) { - input = input.replace("0", "ZERO") - .replace("1", "UN") - .replace("2", "DEUX") - .replace("3", "TROIS") - .replace("4", "QUATRE") - .replace("5", "CINQ") - .replace("6", "SIX") - .replace("7", "SEPT") - .replace("8", "HUIT") - .replace("9", "NEUF"); - return input; - } - } - - /** - * Concrete implementation of an english InputPreparer - */ - public static class ReplaceNumbersEnglish extends InputPreparer { -// --Commented out by Inspection START (07.11.15 19:47): -// public ReplaceNumbersEnglish() { -// this.child = null; -// } -// --Commented out by Inspection STOP (07.11.15 19:47) - - public ReplaceNumbersEnglish(InputPreparer child) { - this.child = child; - } - - protected String prepare(String input) { - input = input.replace("0", "ZERO") - .replace("1", "ONE") - .replace("2", "TWO") - .replace("3", "THREE") - .replace("4", "FOUR") - .replace("5", "FIVE") - .replace("6", "SIX") - .replace("7", "SEVEN") - .replace("8", "EIGHT") - .replace("9", "NINE"); - return input; - } - } - - /** - * Concrete implementation of a spanish InputPreparer - */ - public static class ReplaceNumbersSpanish extends InputPreparer { -// --Commented out by Inspection START (07.11.15 19:47): -// public ReplaceNumbersSpanish() { -// this.child = null; -// } -// --Commented out by Inspection STOP (07.11.15 19:47) - - public ReplaceNumbersSpanish(InputPreparer child) { - this.child = child; - } - - protected String prepare(String input) { - input = input.replace("0", "CERO") - .replace("1", "UNO") - .replace("2", "DOS") - .replace("3", "TRES") - .replace("4", "CUATRO") - .replace("5", "CINCO") - .replace("6", "SEIS") - .replace("7", "SIETE") - .replace("8", "OCHO") - .replace("9", "NUEVE"); - return input; - } - } - - /** - * Concrete implementation of a spanish InputPreparer - */ - public static class ReplaceNumbersItalian extends InputPreparer { -// --Commented out by Inspection START (07.11.15 19:47): -// public ReplaceNumbersItalian() { -// this.child = null; -// } -// --Commented out by Inspection STOP (07.11.15 19:47) - - public ReplaceNumbersItalian(InputPreparer child) { - this.child = child; - } - - protected String prepare(String input) { - input = input.replace("0", "ZERO") - .replace("1", "UNO") - .replace("2", "DUE") - .replace("3", "TRE") - .replace("4", "QUATTRO") - .replace("5", "CINQUE") - .replace("6", "SEI") - .replace("7", "SETTE") - .replace("8", "OTTO") - .replace("9", "NOVE"); - return input; - } - } - - /** - * "Final Stage" of Input preparing. This should always be called last - * (choose this as the inner most capsule of InputPreparers) - * This cant have child InputPreparers. - * This Input preparer removes all characters from the string besides A..Z - */ - public static class RemoveIllegalCharacters extends InputPreparer - { - public RemoveIllegalCharacters() - { - this.child = null; - } - - protected String prepare(String in) - { - String out = ""; - for(char c : in.toUpperCase().toCharArray()) + if (x >= 65 && x <= 90) //x in [A..Z] { - if(c>=65 && c<=90) out = out+c; + output = output + x; } - return out; + else if (x >= 48 && x <= 57) //x in [0..9] + { + output = output + replaceNumber(x); + } + //x is special symbol + else if (x != ' ') + { + output = output + 'X'; + } + } + return output; + } + + /** + * Abstract method that spells numbers in a certain language specified by the implementation + * @param input character + * @return spelled number + */ + public abstract String replaceNumber(char input); + + /** + * Factory method that creates a specific InputPreparer + * @param language language alias that specifies the language (de,fr,en) + * @return concrete InputPreparer + */ + public static InputPreparer createInputPreparer(String language) + { + switch (language) + { + case "de": return new InputPreparerGerman(); + case "fr": return new InputPreparerFrench(); + default: return new InputPreparerEnglish(); + } + } +} + +/** + * Concrete implementation of a german InputPreparer + */ +class InputPreparerGerman extends InputPreparer +{ + @Override + public String replaceNumber(char input) { + switch (input) + { + case '0': return "NULL"; + case '1': return "EINS"; + case '2': return "ZWEI"; + case '3': return "DREI"; + case '4': return "VIER"; + case '5': return "FUENF"; + case '6': return "SECHS"; + case '7': return "SIEBEN"; + case '8': return "ACHT"; + default: return "NEUN"; + } + } +} + +/** + * Concrete implementation of an english InputPreparer + */ +class InputPreparerEnglish extends InputPreparer +{ + @Override + public String replaceNumber(char input) + { + switch (input) { + case '0': return "ZERO"; + case '1': return "ONE"; + case '2': return "TWO"; + case '3': return "THREE"; + case '4': return "FOUR"; + case '5': return "FIVE"; + case '6': return "SIX"; + case '7': return "SEVEN"; + case '8': return "EIGHT"; + default: return "NINE"; + } + } +} + +/** + * Concrete implementation of a french InputPreparer + */ +class InputPreparerFrench extends InputPreparer +{ + + @Override + public String replaceNumber(char input) + { + switch (input) { + case '0': return "ZERO"; + case '1': return "UN"; + case '2': return "DEUX"; + case '3': return "TROIS"; + case '4': return "QUATRE"; + case '5': return "CINQ"; + case '6': return "SIX"; + case '7': return "SEPT"; + case '8': return "HUIT"; + default: return "NEUF"; } } } \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/EntryWheel.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/EntryWheel.java deleted file mode 100644 index f326e19..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/EntryWheel.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma.parts; - -/** - * Implementation of several EntryWheels - * Copyright (C) 2015 Paul Schaub - */ -public class EntryWheel -{ - private final int type; - private final String name; - private int index; - private final String summary; - private final Integer[] connections; - private final Integer[] reversedConnections; - - EntryWheel(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections) - { - this.type = type; - this.name = name; - this.summary = summary; - this.connections = connections; - this.reversedConnections = reversedConnections; - } - - /** - * Returns a new EntryWheel of the same type as the callee - * @return new EntryWheel - */ - public EntryWheel getInstance() - { - return createEntryWheel(this.type).setIndex(this.getIndex()); - } - - /** - * Encrypt an input signal via the internal wiring in "forward" direction towards the reflector - * (using connections) - * @param input signal - * @return encrypted signal - */ - public int encryptForward(int input) - { - return this.connections[normalize(input)]; - } - - private int normalize(int input) - { - return (input+this.connections.length)%this.connections.length; - } - - public EntryWheel setIndex(int index) - { - this.index = index; - return this; - } - - public int getIndex() - { - return this.index; - } - - /** - * Encrypt an input signal via the internal wiring in "backwards" direction (using - * reversedConnections) - * @param input signal - * @return encrypted signal - */ - public int encryptBackward(int input) - { - return this.reversedConnections[normalize(input)]; - } - - private EntryWheel createEntryWheel(int type) - { - switch(type) - { - case 1: - return new EntryWheel_QWERTZ(); - case 2: - return new EntryWheel_T(); - default: return new EntryWheel_ABCDEF(); - } - } - - public static class EntryWheel_ABCDEF extends EntryWheel - { - public EntryWheel_ABCDEF() - { - super(0, "ABCDEF", "abcdefghijklmnopqrstuvwxyz", - new Integer[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}, - new Integer[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}); - } - } - - /** - * EntryWheel as used in the Enigma models D, K, G - * Q W E R T Z U I O A S D F G H J K P Y X C V B N M L - */ - public static class EntryWheel_QWERTZ extends EntryWheel - { - public EntryWheel_QWERTZ() - { - super(1, "QWERTZ", "qwertzuioasdfghjkpyxcvbnml", - new Integer[]{9,22,20,11,2,12,13,14,7,15,16,25,24,23,8,17,0,3,10,4,6,21,1,19,18,5}, - new Integer[]{16,22,4,17,19,25,20,8,14,0,18,3,5,6,7,9,10,15,24,23,2,21,1,13,12,11}); - } - } - - /** - * EntryWheel as used only in the Enigma Type T Tirpitz - * K Z R O U Q H Y A I G B L W V S T D X F P N M C J E - */ - public static class EntryWheel_T extends EntryWheel - { - public EntryWheel_T() - { - super(2, "KZROUQ", "kzrouqhyaigblwvstdxfpnmcje", - new Integer[]{8,11,23,17,25,19,10,6,9,24,0,12,22,21,3,20,5,2,15,16,4,14,13,18,7,1}, - new Integer[]{10,25,17,14,20,16,7,24,0,8,6,1,11,22,21,18,19,3,23,5,15,13,12,2,9,4}); - } - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Plugboard.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Plugboard.java deleted file mode 100644 index 2204b47..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Plugboard.java +++ /dev/null @@ -1,219 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma.parts; - -import android.util.Log; - -import java.math.BigInteger; -import java.util.Arrays; -import java.util.Random; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; - -/** - * Plugboard of the enigma - * Copyright (C) 2015 Paul Schaub - */ -public class Plugboard -{ - private static final int[] empty = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; - private int[] plugs; - - public Plugboard() - { - plugs = Arrays.copyOf(empty, empty.length); - } - - public Plugboard(int[] conf) - { - this.plugs = conf; - } - - public void setConfiguration(int[] conf) - { - this.plugs = conf; - } - - @SuppressWarnings("UnusedReturnValue") - public BigInteger setConfiguration(BigInteger b) - { - String s = ""; - - int x; - while((x = Enigma.getValue(b, 27)) != 26 && b.compareTo(BigInteger.ZERO) > 1) - { - s = ((char) (x+65))+s; - b = Enigma.removeDigit(b, 27); - } - this.setConfiguration(stringToConfiguration(s)); - return b; - } - - public int[] getConfiguration() - { - return this.plugs; - } - - /** - * Encrypt input via plugboard connections - * @param input input symbol to encryptString - * @return encrypted symbol - */ - public int encrypt(int input) - { - return plugs[(input+plugs.length)%plugs.length]; - } - - /** - * Interpret a String of Pairs as a configuration for the plugboard - * Any char with an even index is connected to the successor. - * in must not contain duplicates! - * @param in String representation of pairs (eg. AXBHCS...) - * @return connections as int[] - */ - public static int[] stringToConfiguration(String in) - { - String pairs = trimString(new InputPreparer.RemoveIllegalCharacters().prepareString(in)); - int[] out = Arrays.copyOf(empty, empty.length); - //Check if in is too long or odd - int l = pairs.length(); - if(l>1 && (pairs.length() > 26 || pairs.length()/2 == (pairs.length()-1)/2)) - { - //Odd length. remove last char. Information loss! - pairs = pairs.substring(0,pairs.length()-1); - } - //Modify out - for(int i=0; i 1) - { - s = ((char) (x+65))+s; - b = Enigma.removeDigit(b, 27); - } - Log.d(MainActivity.APP_ID, "Restored: "+s); - return stringToConfiguration(s); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Reflector.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Reflector.java deleted file mode 100644 index d74d823..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Reflector.java +++ /dev/null @@ -1,355 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma.parts; - -import android.util.Log; - -import java.math.BigInteger; -import java.util.Arrays; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.enigma.Enigma; - -/** - * Reflector of the enigma machine. - * The reflector was used to reflect the scrambled signal at the end of the wiring back to - * go through another (reversed but not inverting) process of scrambling. - * Copyright (C) 2015 Paul Schaub - */ -public class Reflector -{ - private final int type; - private final String name; - private int index; - private final String summary; - private int[] connections; - private int rotation; - private int ringSetting; - - /** - * This constructor is not accessible from outside this class file. - * Use the one of the createReflector* methods instead to create concrete Reflectors from - * outside this class file - * @param type name indicator of the reflector - * @param connections wiring of the reflector as Integer array - */ - Reflector(int type, String name, String summary, int[] connections) - { - this.type = type; - this.name = name; - this.summary = summary; - this.connections = connections; - } - - public Reflector getInstance() - { - //noinspection ConstantConditions - return createReflector(this.type).setIndex(this.getIndex()); - } - - public Reflector getInstance(int rotation, int ringSetting) - { - //noinspection ConstantConditions - return createReflector(this.type).setIndex(this.getIndex()) - .setRotation(rotation).setRingSetting(ringSetting); - } - - public Reflector setIndex(int index) - { - this.index = index; - return this; - } - - public int getIndex() - { - return this.index; - } - - public int getRotation() - { - return rotation; - } - - public int getRingSetting() - { - return ringSetting; - } - - public Reflector setRotation(int rotation) - { - this.rotation = rotation; - return this; - } - - public Reflector setRingSetting(int ringSetting) - { - this.ringSetting = ringSetting; - return this; - } - - public Reflector setConfiguration(int[] c) - { - this.connections = c; - return this; - } - - @SuppressWarnings("UnusedReturnValue") - public BigInteger setConfiguration(BigInteger b) - { - String s = ""; - - int x; - while((x = Enigma.getValue(b, 27)) != 26 || b.compareTo(BigInteger.ZERO) > 1) - { - s = ((char) (x+65))+s; - b = Enigma.removeDigit(b, 27); - } - Log.d(MainActivity.APP_ID, "Restored: " + s); - this.setConfiguration(Plugboard.stringToConfiguration(s)); - return b; - } - - public int[] getConfiguration() - { - return connections; - } - - /** - * Factory method to create reflectors. - * @param type name of the created reflector - * 1 -> ReflectorA - * 2 -> ReflectorB - * 3 -> ReflectorC - * 4 -> ReflectorThinB - * 5 -> ReflectorThinC - * 6 -> ReflectorEnigma_D_KD_G31 - * 7 -> Reflector_K - * 8 -> Reflector_T - * 9 -> Reflector_G312 - * 10 -> Reflector_G260 - * 11 -> Reflector_R - * default -> ReflectorB - * @return Reflector - */ - private static Reflector createReflector(int type) - { - switch (type) - { - case 0: return new Reflector_A(); - case 1: return new Reflector_B(); - case 2: return new Reflector_C(); - case 10: return new Reflector_Thin_B(); - case 11: return new ReflectorThinC(); - case 20: return new ReflectorEnigma_D_G31(); - case 21: return new ReflectorEnigma_KD(); - case 30: return new Reflector_G312(); - case 40: return new Reflector_K_G260(); - case 50: return new Reflector_R(); - case 60: return new ReflectorEnigma_T(); - - default: - Log.e(MainActivity.APP_ID," Tried to create Reflector of invalid name "+type); - return null; - } - } - - /** - * Substitute an input signal via the wiring of the reflector with a different (!) output. - * The output MUST not be equal to the input for any input, since this was not possible - * due to the electronic implementation of the historical enigma machine. - * @param input input signal - * @return encrypted (substituted) output - */ - public int encrypt(int input) - { - return this.connections[normalize(input)]; - } - - /** - * Return the size (ie the number of wires/length of the connections array) of the reflector - * @return size - */ - private int getRotorSize() - { - return this.connections.length; - } - - /** - * Normalize the input. - * Normalizing means keeping the input via modulo in the range from 0 to n-1, where n is equal - * to the size of the reflector. This is necessary since java allows negative modulo values, - * which can break this implementation - * @param input input signal - * @return "normalized" input signal - */ - private int normalize(int input) - { - return (input + this.getRotorSize()) % this.getRotorSize(); - } - - /** - * Concrete implementation of ReflectorA - * Used in Enigma I - * AE BJ CM DZ FL GY HX IV KW NR OQ PU ST - */ - public static class Reflector_A extends Reflector - { - public Reflector_A() - { - super(0, "A", "EJMZALYXVBWFCRQUONTSPIKHGD", - new int[]{4,9,12,25,0,11,24,23,21,1,22,5,2,17,16,20,14,13,19,18,15,8,10,7,6,3}); - } - } - - /** - * Concrete implementation of ReflectorB - * Used in Enigma I, M3 - * AY BR CU DH EQ FS GL IP JX KN MO TZ VW - */ - public static class Reflector_B extends Reflector - { - public Reflector_B() - { - super(1, "B", "YRUHQSLDPXNGOKMIEBFZCWVJAT", - new int[]{24,17,20,7,16,18,11,3,15,23,13,6,14,10,12,8,4,1,5,25,2,22,21,9,0,19}); - } - } - - /** - * Concrete implementation of ReflectorC - * Used in Enigma I, M3 - * AF BV CP DJ EI GO HY KR LZ MX NW QT SU - */ - public static class Reflector_C extends Reflector - { - public Reflector_C() - { - super(2, "C", "FVPJIAOYEDRZXWGCTKUGSBNMHL", - new int[]{5,21,15,9,8,0,14,24,4,3,17,25,23,22,6,2,19,10,20,16,18,1,13,12,7,11}); - } - } - - /** - * Concrete implementation of thin reflector name b (not equal to normal name b!) - * When used with Rotor Beta on rotation 0, the pair was equivalent to normal reflector B - * S->Beta->ThinB->Beta'->X == X->UKWB->S - * Used in Enigma M4 - * E N K Q A U Y W J I C O P B L M D X Z V F T H R G S - */ - public static class Reflector_Thin_B extends Reflector - { - public Reflector_Thin_B() - { - super(10, "Thin-B", "ENKQAUYWJICOPBLMDXZVFTHRGS", - new int[]{4,13,10,16,0,20,24,22,9,8,2,14,15,1,11,12,3,23,25,21,5,19,7,17,6,18}); - } - } - - /** - * Concrete implementation of thin reflector name c (not equal to normal name c!) - * When used with Rotor Gamma on rotation 0, the pair was equivalent to normal reflector C - * S->Gamma->ThinC->Gamma'->X == X->UKWC->S - * Used in Enigma M4 - * R D O B J N T K V E H M L F C W Z A X G Y I P S U Q - */ - public static class ReflectorThinC extends Reflector - { - public ReflectorThinC() - { - super(11, "ThinC", "RDOBJNTKVEHMLFCWZAXGYIPSUQ", - new int[]{17,3,14,1,9,13,19,10,21,4,7,12,11,5,2,22,25,0,23,6,24,8,15,18,20,16}); - } - } - - /** - * Pluggable Reflector of the Enigma machine of name D and G31 - * Standard wiring: AI,BM,CE,DT,FG,HR,JY,KS,LQ,NZ,OX,PW,UV - * Has additional ringSetting and can rotate - */ - public static class ReflectorEnigma_D_G31 extends Reflector - { - public static final int[] defaultWiring_D_G31 = {8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}; - public ReflectorEnigma_D_G31() - { - super(20, "Ref-D", "Default: IMETCGFRAYSQBZXWLHKDVUPOJN", Arrays.copyOf(defaultWiring_D_G31,defaultWiring_D_G31.length)); - } - } - - /** - * Pluggable Reflector as used in the Enigma of Type KD - * Standard wiring: KOTVPNLMJIAGHFBEWYXCZDQSRU - * Has additional ringSetting and can rotate - */ - public static class ReflectorEnigma_KD extends Reflector - { - public static final int[] defaultWiring_KD = {10,14,19,21,15,13,11,12,9,8,0,6,7,5,1,4,22,24,23,2,25,3,16,18,17,20}; - public ReflectorEnigma_KD() - { - super(21, "Ref-KD", "Default: KOTVPNLMJIAGHFBEWYXCZDQSRU", Arrays.copyOf(defaultWiring_KD, defaultWiring_KD.length)); - } - } - - /** - * Reflector as used in the Enigma name G-312 Abwehr - * R U L Q M Z J S Y G O C E T K W D A H N B X P V I F - */ - public static class Reflector_G312 extends Reflector - { - public Reflector_G312() - { - super(30, "Ref-G312", "RULQMZJSYGOCETKWDAHNBXPVIF", - new int[]{17,20,11,16,12,25,9,18,24,6,14,2,4,19,10,22,3,0,7,13,1,23,15,21,8,5}); - } - } - - /** - * Reflector as used in the Enigma name G-260 Abwehr - * I M E T C G F R A Y S Q B Z X W L H K D V U P O J N - */ - public static class Reflector_K_G260 extends Reflector - { - public Reflector_K_G260() - { - super(40,"Ref-K/G260", "IMETCGFRAYSQBZXWLHKDVUPOJN", - new int[]{8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}); - } - } - - /** - * Reflector as used in the Enigma Type R "Rocket" (Reichsbahn) - * Q Y H O G N E C V P U Z T F D J A X W M K J S R B L - */ - public static class Reflector_R extends Reflector - { - public Reflector_R() - { - super(50, "Ref-R", "QYHOGNECVPUZTFDJAXWMKJSRBL", - new int[]{16,24,7,14,6,13,4,2,21,15,20,25,19,5,3,9,0,23,22,12,10,8,18,17,1,11}); - } - } - - /** - * Reflector as used in the Enigma name T (Tirpitz) - * G E K P B T A U M O C N I L J D X Z Y F H W V Q S R - */ - public static class ReflectorEnigma_T extends Reflector - { - public ReflectorEnigma_T() - { - super(60, "Ref-T", "GEKPBTAUMOCNILJDXZYFHWVQSR", - new int[]{6,4,10,15,1,19,0,20,12,14,2,13,8,11,9,3,23,25,24,5,7,22,21,16,18,17}); - } - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Rotor.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Rotor.java deleted file mode 100644 index a797fcf..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Rotor.java +++ /dev/null @@ -1,1029 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.enigma.parts; - -import android.util.Log; - -import de.vanitasvitae.enigmandroid.MainActivity; - -/** - * Rotor super class and inner concrete implementations - * The rotors were the key feature of the enigma used to scramble up input signals into - * encrypted signals difficult to predict. The rotors rotated to achieve a poly-alphabetic - * substitution which was hard to break. Each signal passes the rotor twice. Once in "forward"- - * direction and once in "backwards"-direction. There was a set of 3 out of 5 rotors inside the - * enigma machine M4. - * Copyright (C) 2015 Paul Schaub - */ -public abstract class Rotor -{ - /** Number of the rotor (used internally to create the Rotor via createRotor() ) */ - private final int type; - - /** Identifier of the Rotor */ - private final String name; - - /** Index of the Rotor in the parent machine's selection Spinner */ - private int index; - - /** Summary of the connections (internal wiring) */ - private final String summary; - - /** Wiring of the rotor when the signal passes the first time */ - private final Integer[] connections; - - /** Wiring of the rotor when the signal passes the second time (inverse of the first time) */ - private final Integer[] reversedConnections; - - /** When the Rotor is at this Position and jumps one over, it also turns the next */ - private final Integer[] turnOverNotches; - - /** Offset of the labeled ring of the rotor */ - private int ringSetting; - - /** Rotation of the rotor */ - private int rotation; - - /** - * This constructor is not accessible from outside this class file. - * Use one of the createRotor* factory methods instead to create concrete Rotors. - * Note that connections and reversedConnections MUST be of the same size and that - * neither connections nor reversedConnections respectively MUST have any number between - * 0 and connections.length-1 only once (ie they represent permutations) - * @param name name indicator - * @param connections wiring of the rotor as Integer array - * @param reversedConnections inverse wiring used to encryptString in the opposite direction - * (connections[reversedConnections[i]] = i - * for all i in 0..getRotorSize()-1. - * @param turnOverNotches Position(s) of the turnover notch(es) - * @param ringSetting setting of the ring that holds the letters - * @param rotation rotation of the rotor - */ - Rotor(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections, - Integer[] turnOverNotches, int ringSetting, int rotation) - { - this.type = type; - this.name = name; - this.summary = summary; - this.connections = connections; - this.reversedConnections = reversedConnections; - this.turnOverNotches = turnOverNotches; - this.ringSetting = ringSetting; - this.rotation = rotation; - } - - /** - * Create a new Rotor of the same type as the callee. - * @param rotation rotation of the new Rotor - * @param ringSetting ringSetting of the new Rotor - * @return new Rotor - */ - public Rotor getInstance(int rotation, int ringSetting) - { - //noinspection ConstantConditions - return createRotor(this.type, rotation, ringSetting).setIndex(this.getIndex()); - } - - /** - * Create a new Rotor of the same type as the callee. - * @return new Rotor - */ - public Rotor getInstance() - { - //noinspection ConstantConditions - return createRotor(this.type, 0, 0).setIndex(this.getIndex()); - } - - private static Rotor createRotor(int type, int rotation, int ringSetting) - { - Log.d(MainActivity.APP_ID, "Rotor creation: "+type); - switch (type) - { - case 0: return new Rotor_I(rotation, ringSetting); - case 1: return new Rotor_II(rotation, ringSetting); - case 2: return new Rotor_III(rotation, ringSetting); - case 3: return new Rotor_IV(rotation, ringSetting); - case 4: return new Rotor_V(rotation, ringSetting); - case 5: return new Rotor_VI(rotation, ringSetting); - case 6: return new Rotor_VII(rotation, ringSetting); - case 7: return new Rotor_VIII(rotation, ringSetting); - - case 10: return new Rotor_M4_Beta(rotation, ringSetting); - case 11: return new Rotor_M4_Gamma(rotation, ringSetting); - - case 20: return new Rotor_G31_I(rotation, ringSetting); - case 21: return new Rotor_G31_II(rotation, ringSetting); - case 22: return new Rotor_G31_III(rotation, ringSetting); - - case 30: return new Rotor_G312_I(rotation, ringSetting); - case 31: return new Rotor_G312_II(rotation, ringSetting); - case 32: return new Rotor_G312_III(rotation, ringSetting); - - case 40: return new Rotor_G260_I(rotation, ringSetting); - case 41: return new Rotor_G260_II(rotation, ringSetting); - case 42: return new Rotor_G260_III(rotation, ringSetting); - - case 50: return new Rotor_K_D_I(rotation, ringSetting); - case 51: return new Rotor_K_D_II(rotation, ringSetting); - case 52: return new Rotor_K_D_III(rotation, ringSetting); - - case 60: return new Rotor_KD_I(rotation, ringSetting); - case 61: return new Rotor_KD_II(rotation, ringSetting); - case 62: return new Rotor_KD_III(rotation, ringSetting); - - case 70: return new Rotor_KSwiss_Standard_I(rotation, ringSetting); - case 71: return new Rotor_KSwiss_Standard_II(rotation, ringSetting); - case 72: return new Rotor_KSwiss_Standard_III(rotation, ringSetting); - - case 80: return new Rotor_K_Swiss_Airforce_I(rotation, ringSetting); - case 81: return new Rotor_K_Swiss_Airforce_II(rotation, ringSetting); - case 82: return new Rotor_K_Swiss_Airforce_III(rotation, ringSetting); - - case 90: return new Rotor_R_I(rotation, ringSetting); - case 91: return new Rotor_R_II(rotation, ringSetting); - case 92: return new Rotor_R_III(rotation, ringSetting); - - case 100: return new Rotor_T_I(rotation, ringSetting); - case 101: return new Rotor_T_II(rotation, ringSetting); - case 102: return new Rotor_T_III(rotation, ringSetting); - case 103: return new Rotor_T_IV(rotation, ringSetting); - case 104: return new Rotor_T_V(rotation, ringSetting); - case 105: return new Rotor_T_VI(rotation, ringSetting); - case 106: return new Rotor_T_VII(rotation, ringSetting); - case 107: return new Rotor_T_VIII(rotation, ringSetting); - - default: Log.e(MainActivity.APP_ID," Tried to create Rotor of invalid name "+type); - return null; - } - } - - /** - * Encrypt an input signal via the internal wiring in "forward" direction towards the reflector - * (using connections) - * @param input signal - * @return encrypted signal - */ - public int encryptForward(int input) - { - return this.connections[normalize(input)]; - } - - /** - * Encrypt an input signal via the internal wiring in "backwards" direction (using - * reversedConnections) - * @param input signal - * @return encrypted signal - */ - public int encryptBackward(int input) - { - return this.reversedConnections[normalize(input)]; - } - - /** - * Return the name indicator (usually 1..5) - * @return name indicator - */ - public String getName() - { - return this.name; - } - - /** - * Return the index of this Rotor - * @return index - */ - public int getIndex() - { - return this.index; - } - - /** - * Set the index of this Rotor. The index refers to the selector Spinner of the machine, this - * Rotor is used in. - * @param index index in the Spinner - * @return this - */ - public Rotor setIndex(int index) - { - this.index = index; - return this; - } - - public Rotor setRotation(int r) - { - this.rotation = r%this.getRotorSize(); - return this; - } - - public Rotor setRingSetting(int r) - { - this.ringSetting = r%this.getRotorSize(); - return this; - } - - /** - * Return the current rotation of the rotor. - * The rotation consists of the actual rotation - the ringSetting - * @return rotation-ringSetting - */ - public int getRotation() - { - return this.rotation; - } - - /** - * Increment rotation of the rotor by one. - */ - public Rotor rotate() - { - this.rotation = normalize(this.getRotation()+1); - return this; - } - - /** - * Return true, if the rotor is at a position, where it turns over the next rotor by one - * @return rotation==turnOverNotch - */ - public boolean isAtTurnoverPosition() - { - for(int x : getTurnOverNotches()) - { - //if(x == this.rotation + this.ringSetting) return true; - if(x == this.rotation) return true; - } - return false; - } - - /** - * Return true, if the rotor is in a position where the double turn anomaly happens. - * The double turn anomaly (german: Doppelsprung-Anomalie) is an anomaly in the rotor movement - * caused by the mechanical implementation of the enigma. - * Whenever the rightmost rotor turns the middle rotor AND the middle rotor is only one move - * from turning the leftmost rotor, the middle rotor turns again with the next character. - * So technically there are only 26*25*26 possible rotor settings for any but firmly 3 rotors. - * @return rotation == turnOverNotch-1 - */ - public boolean doubleTurnAnomaly() - { - for(int x : getTurnOverNotches()) - { - if(this.rotation == x-1) return true; - } - return false; - } - - /** - * Returns the positions of the turnover notches in a array - * @return turnOverNotches - */ - private Integer[] getTurnOverNotches() - { - return this.turnOverNotches; - } - - /** - * Return ringSettings of the rotor - * @return ringSetting - */ - public int getRingSetting() - { - return this.ringSetting; - } - - - /** - * Returns the size (ie the number of wires/size of the connections array) - * of the rotor - * @return size - */ - private int getRotorSize() - { - return this.connections.length; - } - - /** - * Normalize the input. - * Normalizing means keeping the input via modulo in the range from 0 to n-1, where n is equal - * to the size of the rotor. This is necessary since java allows negative modulo values, - * which can break this implementation - * @param input input signal - * @return "normalized" input signal - */ - public int normalize(int input) - { - return (input+this.getRotorSize())%this.getRotorSize(); - } - - /** - * Concrete implementation of Rotor of name 1 (I) - * Used in Enigma I, M3, M4 - * E K M F L G D Q V Z N T O W Y H X U S P A I B R C J - */ - public static class Rotor_I extends Rotor - { - public Rotor_I(int rotation, int ringSetting) - { - super(0, "I", "EKMFLGDQVZNTOWYHXUSPAIBRCJ", - new Integer[]{4, 10, 12, 5, 11, 6, 3, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9}, - new Integer[]{20, 22, 24, 6, 0, 3, 5, 15, 21, 25, 1, 4, 2, 10, 12, 19, 7, 23, 18, 11, 17, 8, 13, 16, 14, 9}, - new Integer[]{17}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 2 (II) - * Used in Enigma I, M3, M4 - * A J D K S I R U X B L H W T M C Q G Z N P Y F V O E - */ - public static class Rotor_II extends Rotor - { - public Rotor_II(int rotation, int ringSetting) - { - super(1, "II", "AJDKSIRUXBLHWTMCQGZNPYFVOE", - new Integer[]{0, 9, 3, 10, 18, 8, 17, 20, 23, 1, 11, 7, 22, 19, 12, 2, 16, 6, 25, 13, 15, 24, 5, 21, 14, 4}, - new Integer[]{0, 9, 15, 2, 25, 22, 17, 11, 5, 1, 3, 10, 14, 19, 24, 20, 16, 6, 4, 13, 7, 23, 12, 8, 21, 18}, - new Integer[]{5}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 3 (III) - * Used in Enigma I, M3, M4 - * B D F H J L C P R T X V Z N Y E I W G A K M U S Q O - */ - public static class Rotor_III extends Rotor - { - public Rotor_III(int rotation, int ringSetting) - { - super(2, "III", "BDFHJLCPRTXVZNYEIWGAKMUSQO", - new Integer[]{1, 3, 5, 7, 9, 11, 2, 15, 17, 19, 23, 21, 25, 13, 24, 4, 8, 22, 6, 0, 10, 12, 20, 18, 16, 14}, - new Integer[]{19, 0, 6, 1, 15, 2, 18, 3, 16, 4, 20, 5, 21, 13, 25, 7, 24, 8, 23, 9, 22, 11, 17, 10, 14, 12}, - new Integer[]{22}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 4 (IV) - * Used in Enigma M3, M4 - * E S O V P Z J A Y Q U I R H X L N F T G K D C M W B - */ - public static class Rotor_IV extends Rotor - { - public Rotor_IV(int rotation, int ringSetting) - { - super(3, "IV", "ESOVPZJAYQUIRHXLNFTGKDCMWB", - new Integer[]{4, 18, 14, 21, 15, 25, 9, 0, 24, 16, 20, 8, 17, 7, 23, 11, 13, 5, 19, 6, 10, 3, 2, 12, 22, 1}, - new Integer[]{7, 25, 22, 21, 0, 17, 19, 13, 11, 6, 20, 15, 23, 16, 2, 4, 9, 12, 1, 18, 10, 3, 24, 14, 8, 5}, - new Integer[]{10}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 5 (V) - * Used in Enigma M3, M4 - * V Z B R G I T Y U P S D N H L X A W M J Q O F E C K - */ - public static class Rotor_V extends Rotor - { - public Rotor_V(int rotation, int ringSetting) - { - super(4, "V", "VZBRGITYUPSDNHLXAWMJQOFECK", - new Integer[]{21, 25, 1, 17, 6, 8, 19, 24, 20, 15, 18, 3, 13, 7, 11, 23, 0, 22, 12, 9, 16, 14, 5, 4, 2, 10}, - new Integer[]{16, 2, 24, 11, 23, 22, 4, 13, 5, 19, 25, 14, 18, 12, 21, 9, 20, 3, 10, 6, 8, 0, 17, 15, 7, 1}, - new Integer[]{0}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 6 (VI) - * Used in Enigma M3, M4 - * J P G V O U M F Y Q B E N H Z R D K A S X L I C T W - */ - public static class Rotor_VI extends Rotor - { - public Rotor_VI(int rotation, int ringSetting) - { - super(5, "VI", "JPGVOUMFYQBENHZRDKASXLICTW", - new Integer[]{9,15,6,21,14,20,12,5,24,16,1,4,13,7,25,17,3,10,0,18,23,11,8,2,19,22}, - new Integer[]{18,10,23,16,11,7,2,13,22,0,17,21,6,12,4,1,9,15,19,24,5,3,25,20,8,14}, - new Integer[]{0,13}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 7 (VII) - * Used in Enigma M3, M4 - * N Z J H G R C X M Y S W B O U F A I V L P E K Q D T - */ - public static class Rotor_VII extends Rotor - { - public Rotor_VII(int rotation, int ringSetting) - { - super(6, "VII", "NZJHGRCXMYSWBOUFAIVLPEKQDT", - new Integer[]{13,25,9,7,6,17,2,23,12,24,18,22,1,14,20,5,0,8,21,11,15,4,10,16,3,19}, - new Integer[]{16,12,6,24,21,15,4,3,17,2,22,19,8,0,13,20,23,5,10,25,14,18,11,7,9,1}, - new Integer[]{0,13}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name 8 (VIII) - * Used in Enigma M3, M4 - * F K Q H T L X O C B J S P D Z R A M E W N I U Y G V - */ - public static class Rotor_VIII extends Rotor - { - public Rotor_VIII(int rotation, int ringSetting) - { - super(7, "VIII", "FKQHTLXOCBJSPDZRAMEWNIUYGV", - new Integer[]{5,10,16,7,19,11,23,14,2,1,9,18,15,3,25,17,0,12,4,22,13,8,20,24,6,21}, - new Integer[]{16,9,8,13,18,0,24,3,21,10,1,5,17,20,7,12,2,15,11,4,22,25,19,6,23,14}, - new Integer[]{0,13}, ringSetting, rotation); - } - } - - /** - * Concrete implementation of Rotor of name beta (Griechenwalze Beta) - * Beta was used as a "thin" rotor in the M4. It was thinner than a "normal" rotor, so it - * could be used together with one of the two thin reflectors as one rotor. - * When used together with ReflectorThinB, Beta was equivalent to Reflector B (if rotation == 0) - * That way the M4 was backwards compatible to the M3 - * Used in M4 - */ - public static class Rotor_M4_Beta extends Rotor - { - public Rotor_M4_Beta(int rotation, int ringSetting) - { - super(10, "Beta", "LEYJVCNIXWPBQMDRTAKZGFUHOS", - new Integer[]{11,4,24,9,21,2,13,8,23,22,15,1,16,12,3,17,19,0,10,25,6,5,20,7,14,18}, - new Integer[]{17,11,5,14,1,21,20,23,7,3,18,0,13,6,24,10,12,15,25,16,22,4,9,8,2,19}, - new Integer[]{}, ringSetting, rotation); - } - @Override - public Rotor rotate() - { - //Thin rotors are fixed in position, so they don't rotate - return this; - } - - @Override - public boolean doubleTurnAnomaly() - { - //Nope, no anomaly - return false; - } - } - - /** - * Concrete implementation of Rotor of name gamma (Griechenwalze Gamma) - * Gamma was used as a "thin" rotor in the M4. It was thinner than a "normal" rotor, so it - * could be used together with one of the two thin reflectors as one rotor. - * When used together with ReflectorThinC, Gamma is equivalent to Reflector C - * (if rotation == 0). That way the M4 was backwards compatible to the M3 - * Used in M4 - */ - public static class Rotor_M4_Gamma extends Rotor - { - public Rotor_M4_Gamma(int rotation, int ringSetting) - { - super(11, "Gamma", "FSOKANUERHMBTIYCWLQPZXVGJD", - new Integer[]{5,18,14,10,0,13,20,4,17,7,12,1,19,8,24,2,22,11,16,15,25,23,21,6,9,3}, - new Integer[]{4,11,15,25,7,0,23,9,13,24,3,17,10,5,2,19,18,8,1,12,6,22,16,21,14,20}, - new Integer[]{}, ringSetting, rotation); - } - @Override - public Rotor rotate() - { - //Thin rotors are fixed in position, so they don't rotate - return this; - } - - @Override - public boolean doubleTurnAnomaly() - { - //Thin rotors don't do such weird stuff, they're normal just like you and me. - return false; - } - } - - /** - * Rotor I as used in the Enigma Type G31 Abwehr - * L P G S Z M H A E O Q K V X R F Y B U T N I C J D W - * Turnover T V W X A B C D F G H J L M P Q R - */ - public static class Rotor_G31_I extends Rotor - { - public Rotor_G31_I(int rotation, int ringSetting) - { - super(20, "G31-I", "LPGSZMHAEOQKVXRFYBUTNICJDW", - new Integer[]{11,15,6,18,25,12,7,0,4,14,16,10,21,23,17,5,24,1,20,19,13,8,2,9,3,22}, - new Integer[]{7,17,22,24,8,15,2,6,21,23,11,0,5,20,9,1,10,14,3,19,18,12,25,13,16,4}, - new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type G31 Abwehr - * S L V G B T F X J Q O H E W I R Z Y A M K P C N D U - * Turnover T U W Z A B D E G H I L N O R - */ - public static class Rotor_G31_II extends Rotor - { - public Rotor_G31_II(int rotation, int ringSetting) - { - super(21, "G31_II", "SLVGBTFXJQOHEWIRZYAMKPCNDU", - new Integer[]{18,11,21,6,1,19,5,23,9,16,14,7,4,22,8,17,25,24,0,12,10,15,2,13,3,20}, - new Integer[]{18,4,22,24,12,6,3,11,14,8,20,1,19,23,10,21,9,15,0,5,25,2,13,7,17,16}, - new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type G31 Abwehr - * C J G D P S H K T U R A W Z X F M Y N Q O B V L I E - * Turnover V X Y B F G I L N O S - */ - public static class Rotor_G31_III extends Rotor - { - public Rotor_G31_III(int rotation, int ringSetting) - { - super(22, "G31_III", "CJGDPSHKTURAWZXFMYNQOBVLIE", - new Integer[]{2,9,6,3,15,18,7,10,19,20,17,0,22,25,23,5,12,24,13,16,14,1,21,11,8,4}, - new Integer[]{11,21,0,3,25,15,2,6,24,1,7,23,16,18,20,4,19,10,5,8,9,22,12,14,17,13}, - new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type G312 Abwehr - * D M T W S I L R U Y Q N K F E J C A Z B P G X O H V - * Turnover T V W X A B C D F G H J L M P Q R - */ - public static class Rotor_G312_I extends Rotor - { - public Rotor_G312_I(int rotation, int ringSetting) - { - super(30, "G312-I", "DMTWSILRUYQNKFEJCAZBPGXOHV", - new Integer[]{3,12,19,22,18,8,11,17,20,24,16,13,10,5,4,9,2,0,25,1,15,6,23,14,7,21}, - new Integer[]{17,19,16,0,14,13,21,24,5,15,12,6,1,11,23,20,10,7,4,2,8,25,3,22,9,18}, - new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type G312 Abwehr - * H Q Z G P J T M O B L N C I F D Y A W V E U S R K X - * Turnover T U W Z A B D E G H I L N O R - */ - public static class Rotor_G312_II extends Rotor - { - public Rotor_G312_II(int rotation, int ringSetting) - { - super(31, "G312-II", "HQZGPJTMOBLNCIFDYAWVEUSRKX", - new Integer[]{7,16,25,6,15,9,19,12,14,1,11,13,2,8,5,3,24,0,22,21,4,20,18,17,10,23}, - new Integer[]{17,9,12,15,20,14,3,0,13,5,24,10,7,11,8,4,1,23,22,6,21,19,18,25,16,2}, - new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type G312 Abwehr - * U Q N T L S Z F M R E H D P X K I B V Y G J C W O A - * Turnover V X Y B F G I L N O S - */ - public static class Rotor_G312_III extends Rotor - { - public Rotor_G312_III(int rotation, int ringSetting) - { - super(32, "G312-III", "UQNTLSZFMREHDPXKIBVYGJCWOA", - new Integer[]{20,16,13,19,11,18,25,5,12,17,4,7,3,15,23,10,8,1,21,24,6,9,2,22,14,0}, - new Integer[]{25,17,22,12,10,7,20,11,16,21,15,4,8,2,24,13,1,9,5,3,0,18,23,14,19,6}, - new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type G260 Abwehr - * R C S P B L K Q A U M H W Y T I F Z V G O J N E X D - * Turnover T V W X A B C D F G H J L M P Q R - */ - public static class Rotor_G260_I extends Rotor - { - public Rotor_G260_I(int rotation, int ringSetting) - { - super(40, "G260-I", "RCSPBLKQAUMHWYTIFZVGOJNEXD", - new Integer[]{17,2,18,15,1,11,10,16,0,20,12,7,22,24,19,8,5,25,21,6,14,9,13,4,23,3}, - new Integer[]{8,4,1,25,23,16,19,11,15,21,6,5,10,22,20,3,7,0,2,14,9,18,12,24,13,17}, - new Integer[]{19,21,22,23,0,1,2,3,5,6,7,9,11,12,15,16,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type G260 Abwehr - * W C M I B V P J X A R O S G N D L Z K E Y H U F Q T - * Turnover T U W Z A B D E G H I L N O R - */ - public static class Rotor_G260_II extends Rotor - { - public Rotor_G260_II(int rotation, int ringSetting) - { - super(41, "G260-II", "WCMIBVPJXAROSGNDLZKEYHUFQT", - new Integer[]{22,2,12,8,1,21,15,9,23,0,17,14,18,6,13,3,11,25,10,4,24,7,20,5,16,19}, - new Integer[]{9,4,1,15,19,23,13,21,3,7,18,16,2,14,11,6,24,10,12,25,22,5,0,8,20,17}, - new Integer[]{19,20,22,25,0,1,3,4,6,7,8,11,13,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type G260 Abwehr - * F V D H Z E L S Q M A X O K Y I W P G C B U J T N R - * Turnover V X Y B F G I L N O S - */ - public static class Rotor_G260_III extends Rotor - { - public Rotor_G260_III(int rotation, int ringSetting) - { - super(42, "G260-III", "FVDHZELSQMAXOKYIWPGCBUJTNR", - new Integer[]{5,21,3,7,25,4,11,18,16,12,0,23,14,10,24,8,22,15,6,2,1,20,9,19,13,17}, - new Integer[]{10,20,19,2,5,0,18,3,15,22,13,6,9,24,12,17,8,25,7,23,21,1,16,11,14,4}, - new Integer[]{21,23,24,1,5,6,8,11,13,14,18}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type K,D - * L P G S Z M H A E O Q K V X R F Y B U T N I C J D W - * Turnover Z - */ - public static class Rotor_K_D_I extends Rotor - { - public Rotor_K_D_I(int rotation, int ringSetting) - { - super(50, "K/D-I", "LPGSZMHAEOQKVXRFYBUTNICJDW", - new Integer[]{11,15,6,18,25,12,7,0,4,14,16,10,21,23,17,5,24,1,20,19,13,8,2,9,3,22}, - new Integer[]{7,17,22,24,8,15,2,6,21,23,11,0,5,20,9,1,10,14,3,19,18,12,25,13,16,4}, - new Integer[]{25}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type K,D - * S L V G B T F X J Q O H E W I R Z Y A M K P C N D U - * Turnover F - */ - public static class Rotor_K_D_II extends Rotor - { - public Rotor_K_D_II(int rotation, int ringSetting) - { - super(51, "K/D-II", "SLVGBTFXJQOHEWIRZYAMKPCNDU", - new Integer[]{18,11,21,6,1,19,5,23,9,16,14,7,4,22,8,17,25,24,0,12,10,15,2,13,3,20}, - new Integer[]{18,4,22,24,12,6,3,11,14,8,20,1,19,23,10,21,9,15,0,5,25,2,13,7,17,16}, - new Integer[]{5}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type K,D - * C J G D P S H K T U R A W Z X F M Y N Q O B V L I E - * Turnover O - */ - public static class Rotor_K_D_III extends Rotor - { - public Rotor_K_D_III(int rotation, int ringSetting) - { - super(52, "K/D-III", "CJGDPSHKTURAWZXFMYNQOBVLIE", - new Integer[]{2,9,6,3,15,18,7,10,19,20,17,0,22,25,23,5,12,24,13,16,14,1,21,11,8,4}, - new Integer[]{11,21,0,3,25,15,2,6,24,1,7,23,16,18,20,4,19,10,5,8,9,22,12,14,17,13}, - new Integer[]{14}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type KD - * VEZIOJCXKYDUNTWAPLQGBHSFMR - * Turnover TVZBFIMOR - */ - public static class Rotor_KD_I extends Rotor - { - public Rotor_KD_I(int rotation, int ringSetting) - { - super(60, "KD-I", "VEZIOJCXKYDUNTWAPLQGBHSFMR", - new Integer[]{21,4,25,8,14,9,2,23,10,24,3,20,13,19,22,0,15,11,16,6,1,7,18,5,12,17}, - new Integer[]{15,20,6,10,1,23,19,21,3,5,8,17,24,12,4,16,18,25,22,13,11,0,14,7,9,2}, - new Integer[]{19,21,25,1,5,8,12,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type KD - * HGRBSJZETDLVPMQYCXAOKINFUW - * Turnover TVZBFIMOR - */ - public static class Rotor_KD_II extends Rotor - { - public Rotor_KD_II(int rotation, int ringSetting) - { - super(61, "KD-II", "HGRBSJZETDLVPMQYCXAOKINFUW", - new Integer[]{7,6,17,1,18,9,25,4,19,3,11,21,15,12,16,24,2,23,0,14,10,8,13,5,20,22}, - new Integer[]{18,3,16,9,7,23,1,0,21,5,20,10,13,22,19,12,14,2,4,8,24,11,25,17,15,6}, - new Integer[]{19,21,25,1,5,8,12,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type KD - * NWLHXGRBYOJSAZDVTPKFQMEUIC - * Turnover TVZBFIMOR - */ - public static class Rotor_KD_III extends Rotor - { - public Rotor_KD_III(int rotation, int ringSetting) - { - super(62, "KD-II", "NWLHXGRBYOJSAZDVTPKFQMEUIC", - new Integer[]{13,22,11,7,23,6,17,1,24,14,9,18,0,25,3,21,19,15,10,5,16,12,4,20,8,2}, - new Integer[]{12,7,25,14,22,19,5,3,24,10,18,2,21,0,9,17,20,6,11,16,23,15,1,4,8,13}, - new Integer[]{19,21,25,1,5,8,12,14,17}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type K (Switzerland) - * P E Z U O H X S C V F M T B G L R I N Q J W A Y D K - * Turnover Z - */ - public static class Rotor_KSwiss_Standard_I extends Rotor - { - public Rotor_KSwiss_Standard_I(int rotation, int ringSetting) - { - super(70, "KS-I", "PEZUOHXSCVFMTBGLRINQJWAYDK", - new Integer[]{15,4,25,20,14,7,23,18,2,21,5,12,19,1,6,11,17,8,13,16,9,22,0,24,3,10}, - new Integer[]{22,13,8,24,1,10,14,5,17,20,25,15,11,18,4,0,19,16,7,12,3,9,21,6,23,2}, - new Integer[]{25}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type K (Switzerland) - * Z O U E S Y D K F W P C I Q X H M V B L G N J R A T - * Turnover F - */ - public static class Rotor_KSwiss_Standard_II extends Rotor - { - public Rotor_KSwiss_Standard_II(int rotation, int ringSetting) - { - super(71, "KS-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT", - new Integer[]{25,14,20,4,18,24,3,10,5,22,15,2,8,16,23,7,12,21,1,11,6,13,9,17,0,19}, - new Integer[]{24,18,11,6,3,8,20,15,12,22,7,19,16,21,1,10,13,23,4,25,2,17,9,14,5,0}, - new Integer[]{5}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type K (Switzerland) - * E H R V X G A O B Q U S I M Z F L Y N W K T P D J C - * Turnover O - */ - public static class Rotor_KSwiss_Standard_III extends Rotor - { - public Rotor_KSwiss_Standard_III(int rotation, int ringSetting) - { - super(72, "KS-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC", - new Integer[]{4,7,17,21,23,6,0,14,1,16,20,18,8,12,25,5,11,24,13,22,10,19,15,3,9,2}, - new Integer[]{6,8,25,23,0,15,5,1,12,24,20,16,13,18,7,22,9,2,11,21,10,3,19,4,17,14}, - new Integer[]{14}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type K (Swiss, Airforce) - * PEZUOHXSCVFMTBGLRINQJWAYDK - * Turnover Z - */ - public static class Rotor_K_Swiss_Airforce_I extends Rotor - { - public Rotor_K_Swiss_Airforce_I(int rotation, int ringSetting) - { - super(80, "KSA-I", "PEZUOHXSCVFMTBGLRINQJWAYDK", - new Integer[]{15,4,25,20,14,7,23,18,2,21,5,12,19,1,6,11,17,8,13,16,9,22,0,24,3,10}, - new Integer[]{22,13,8,24,1,10,14,5,17,20,25,15,11,18,4,0,19,16,7,12,3,9,21,6,23,2}, - new Integer[]{25}, ringSetting, rotation); - } - } - /** - * Rotor II as used in the Enigma Type K (Swiss, Airforce) - * ZOUESYDKFWPCIQXHMVBLGNJRAT - * Turnover F - */ - public static class Rotor_K_Swiss_Airforce_II extends Rotor - { - public Rotor_K_Swiss_Airforce_II(int rotation, int ringSetting) - { - super(81, "KSA-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT", - new Integer[]{25,14,20,4,18,24,3,10,5,22,15,2,8,16,23,7,12,21,1,11,6,13,9,17,0,19}, - new Integer[]{24,18,11,6,3,8,20,15,12,22,7,19,16,21,1,10,13,23,4,25,2,17,9,14,5,0}, - new Integer[]{5}, ringSetting, rotation); - } - } - /** - * Rotor III as used in the Enigma Type K (Swiss, Airforce) - * EHRVXGAOBQUSIMZFLYNWKTPDJC - * Turnover O - */ - public static class Rotor_K_Swiss_Airforce_III extends Rotor - { - public Rotor_K_Swiss_Airforce_III(int rotation, int ringSetting) - { - super(82, "KSA-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC", - new Integer[]{4,7,17,21,23,6,0,14,1,16,20,18,8,12,25,5,11,24,13,22,10,19,15,3,9,2}, - new Integer[]{6,8,25,23,0,15,5,1,12,24,20,16,13,18,7,22,9,2,11,21,10,3,19,4,17,14}, - new Integer[]{14}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type R (Rocket) - * JGDQOXUSCAMIFRVTPNEWKBLZYH - * Turnover O - */ - public static class Rotor_R_I extends Rotor - { - public Rotor_R_I(int rotation, int ringSetting) - { - super(90, "R-I", "JGDQOXUSCAMIFRVTPNEWKBLZYH", - new Integer[]{9,6,3,16,14,23,20,18,2,0,12,8,5,17,21,19,15,13,4,22,10,1,11,25,24,7}, - new Integer[]{9,21,8,2,18,12,1,25,11,0,20,22,10,17,4,16,3,13,7,15,6,14,19,5,24,23}, - new Integer[]{14}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type R (Rocket) - * NTZPSFBOKMWRCJDIVLAEYUXHGQ - * Turnover F - */ - public static class Rotor_R_II extends Rotor - { - public Rotor_R_II(int rotation, int ringSetting) - { - super(91, "R-II", "NTZPSFBOKMWRCJDIVLAEYUXHGQ", - new Integer[]{13,19,25,15,18,5,1,14,10,12,22,17,2,9,3,8,21,11,0,4,24,20,23,7,6,16}, - new Integer[]{18,6,12,14,19,5,24,23,15,13,8,17,9,0,7,3,25,11,4,1,21,16,10,22,20,2}, - new Integer[]{5}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type R (Rocket) - * JVIUBHTCDYAKEQZPOSGXNRMWFL - * Turnover Z - */ - public static class Rotor_R_III extends Rotor - { - public Rotor_R_III(int rotation, int ringSetting) - { - super(92, "R-III", "JVIUBHTCDYAKEQZPOSGXNRMWFL", - new Integer[]{9,21,8,20,1,7,19,2,3,24,0,10,4,16,25,15,14,18,6,23,13,17,12,22,5,11}, - new Integer[]{10,4,7,8,12,24,18,5,2,0,11,25,22,20,16,15,13,21,17,6,3,1,23,19,9,14}, - new Integer[]{25}, ringSetting, rotation); - } - } - - /** - * Rotor I as used in the Enigma Type T Tirpitz - * K P T Y U E L O C V G R F Q D A N J M B S W H Z X I - * Turnover X A F L R - */ - public static class Rotor_T_I extends Rotor - { - public Rotor_T_I(int rotation, int ringSetting) - { - super(100, "T-I", "KPTYUELOCVGRFQDANJMBSWHZXI", - new Integer[]{10,15,19,24,20,4,11,14,2,21,6,17,5,16,3,0,13,9,12,1,18,22,7,25,23,8}, - new Integer[]{15,19,8,14,5,12,10,22,25,17,0,6,18,16,7,1,13,11,20,2,4,9,21,24,3,23}, - new Integer[]{23,0,5,11,17}, ringSetting, rotation); - } - } - - /** - * Rotor II as used in the Enigma Type T Tirpitz - * U P H Z L W E Q M T D J X C A K S O I G V B Y F N R - * Turnover X A G M S - */ - public static class Rotor_T_II extends Rotor - { - public Rotor_T_II(int rotation, int ringSetting) - { - super(101, "T-II", "UPHZLWEQMTDJXCAKSOIGVBYFNR", - new Integer[]{20,15,7,25,11,22,4,16,12,19,3,9,23,2,0,10,18,14,8,6,21,1,24,5,13,17}, - new Integer[]{14,21,13,10,6,23,19,2,18,11,15,4,8,24,17,1,7,25,16,9,0,20,5,12,22,3}, - new Integer[]{23,0,6,12,18}, ringSetting, rotation); - } - } - - /** - * Rotor III as used in the Enigma Type T Tirpitz - * Q U D L Y R F E K O N V Z A X W H M G P J B S I C T - * Turnover X A F L R - */ - public static class Rotor_T_III extends Rotor - { - public Rotor_T_III(int rotation, int ringSetting) { - super(102, "T-III", "QUDLYRFEKONVZAXWHMGPJBSICT", - new Integer[]{16,20,3,11,24,17,5,4,10,14,13,21,25,0,23,22,7,12,6,15,9,1,18,8,2,19}, - new Integer[]{13,21,24,2,7,6,18,16,23,20,8,3,17,10,9,19,0,5,22,25,1,11,15,14,4,12}, - new Integer[]{23,0,5,11,17}, ringSetting, rotation); - } - } - - /** - * Rotor IV as used in the Enigma Type T Tirpitz - * C I W T B K X N R E S P F L Y D A G V H Q U O J Z M - * Turnover X A G M S - */ - public static class Rotor_T_IV extends Rotor - { - public Rotor_T_IV(int rotation, int ringSetting) - { - super(103, "T-IV", "CIWTBKXNRESPFLYDAGVHQUOJZM", - new Integer[]{2,8,22,19,1,10,23,13,17,4,18,15,5,11,24,3,0,6,21,7,16,20,14,9,25,12}, - new Integer[]{16,4,0,15,9,12,17,19,1,23,5,13,25,7,22,11,20,8,10,3,21,18,2,6,14,24}, - new Integer[]{23,0,6,12,18}, ringSetting, rotation); - } - } - - /** - * Rotor V as used in the Enigma Type T Tirpitz - * U A X G I S N J B V E R D Y L F Z W T P C K O H M Q - * Turnover Z D G L S - */ - public static class Rotor_T_V extends Rotor - { - public Rotor_T_V(int rotation, int ringSetting) - { - super(104, "T-V", "UAXGISNJBVERDYLFZWTPCKOHMQ", - new Integer[]{20,0,23,6,8,18,13,9,1,21,4,17,3,24,11,5,25,22,19,15,2,10,14,7,12,16}, - new Integer[]{1,8,20,12,10,15,3,23,4,7,21,14,24,6,22,19,25,11,5,18,0,9,17,2,13,16}, - new Integer[]{25,3,6,11,18}, ringSetting, rotation); - } - } - - /** - * Rotor VI as used in the Enigma Type T Tirpitz - * X F U Z G A L V H C N Y S E W Q T D M R B K P I O J - * Turnover Y F J N R - */ - public static class Rotor_T_VI extends Rotor - { - public Rotor_T_VI(int rotation, int ringSetting) - { - super(105, "T-VI", "XFUZGALVHCNYSEWQTDMRBKPIOJ", - new Integer[]{23,5,20,25,6,0,11,21,7,2,13,24,18,4,22,16,19,3,12,17,1,10,15,8,14,9}, - new Integer[]{5,20,9,17,13,1,4,8,23,25,21,6,18,10,24,22,15,19,12,16,2,7,14,0,11,3}, - new Integer[]{24,5,9,13,17}, ringSetting, rotation); - } - } - - /** - * Rotor VII as used in the Enigma Type T Tirpitz - * B J V F T X P L N A Y O Z I K W G D Q E R U C H S M - * Turnover Z D G L S - */ - public static class Rotor_T_VII extends Rotor - { - public Rotor_T_VII(int rotation, int ringSetting) - { - super(106, "T-VII", "BJVFTXPLNAYOZIKWGDQERUCHSM", - new Integer[]{1,9,21,5,19,23,15,11,13,0,24,14,25,8,10,22,6,3,16,4,17,20,2,7,18,12}, - new Integer[]{9,0,22,17,19,3,16,23,13,1,14,7,25,8,11,6,18,20,24,4,21,2,15,5,10,12}, - new Integer[]{25,3,6,11,18}, ringSetting, rotation); - } - } - - /** - * Rotor VIII as used in the Enigma Type T Tirpitz - * Y M T P N Z H W K O D A J X E L U Q V G C B I S F R - * Turnover Y F J N R - */ - public static class Rotor_T_VIII extends Rotor - { - public Rotor_T_VIII(int rotation, int ringSetting) - { - super(107, "T-VIII", "YMTPNZHWKODAJXELUQVGCBISFR", - new Integer[]{24,12,19,15,13,25,7,22,10,14,3,0,9,23,4,11,20,16,21,6,2,1,8,18,5,17}, - new Integer[]{11,21,20,10,14,24,19,6,22,12,8,15,1,4,9,3,17,25,23,2,16,18,7,13,0,5}, - new Integer[]{24,5,9,13,17}, ringSetting, rotation); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java new file mode 100644 index 0000000..638d2ce --- /dev/null +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java @@ -0,0 +1,256 @@ +package de.vanitasvitae.enigmandroid.enigma.rotors; + +/** + * Reflector of the enigma machine. + * The reflector was used to reflect the scrambled signal at the end of the wiring back to + * go through another (reversed but not inverting) process of scrambling. + * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae + */ +public class Reflector +{ + protected String type; + protected int number; + protected int[] connections; + protected int rotation; + protected int ringSetting; + + /** + * This constructor is not accessible from outside this class file. + * Use the one of the createReflector* methods instead to create concrete Reflectors from + * outside this class file + * @param type type indicator of the reflector + * @param connections wiring of the reflector as Integer array + */ + protected Reflector(String type, int number, int[] connections) + { + this.type = type; + this.number = number; + this.connections = connections; + } + + public int getRotation() + { + return rotation; + } + + public int getRingSetting() + { + return ringSetting; + } + + public void setRotation(int rotation) + { + this.rotation = rotation; + } + + public void setRingSetting(int ringSetting) + { + this.ringSetting = ringSetting; + } + + /** + * Factory method to create reflectors. + * @param type type of the created reflector + * 1 -> ReflectorA + * 2 -> ReflectorB + * 3 -> ReflectorC + * 4 -> ReflectorThinB + * 5 -> ReflectorThinC + * 6 -> ReflectorEnigmaD + * 7 -> ReflectorK + * 7 -> ReflectorT + * default -> ReflectorB + * @return Reflector + */ + public static Reflector createReflector(int type) + { + switch (type) + { + case 1: return new ReflectorA(); + case 2: return new ReflectorB(); + case 3: return new ReflectorC(); + case 4: return new ReflectorThinB(); + case 5: return new ReflectorThinC(); + case 6: return new ReflectorEnigmaDKD(); + case 7: return new ReflectorEnigmaK(); + case 8: return new ReflectorEnigmaT(); + default: return new ReflectorB(); + } + } + + /** + * Substitute an input signal via the wiring of the reflector with a different (!) output. + * The output MUST not be equal to the input for any input, since this was not possible + * due to the electronic implementation of the historical enigma machine. + * @param input input signal + * @return encrypted (substituted) output + */ + public int encrypt(int input) + { + return this.connections[normalize(input)]; + } + + /** + * Return the type indicator of the reflector + * @return type + */ + public String getType() + { + return this.type; + } + + public int getNumber() + { + return this.number; + } + + /** + * Return the size (ie the number of wires/length of the connections array) of the reflector + * @return size + */ + private int getRotorSize() + { + return this.connections.length; + } + + /** + * Normalize the input. + * Normalizing means keeping the input via modulo in the range from 0 to n-1, where n is equal + * to the size of the reflector. This is necessary since java allows negative modulo values, + * which can break this implementation + * @param input input signal + * @return "normalized" input signal + */ + private int normalize(int input) + { + return (input + this.getRotorSize()) % this.getRotorSize(); + } + + /** + * Concrete implementation of ReflectorA + * Used in Enigma I + * AE BJ CM DZ FL GY HX IV KW NR OQ PU ST + */ + private static class ReflectorA extends Reflector + { + public ReflectorA() + { + super("A", 1, new int[]{4,9,12,25,0,11,24,23,21,1,22,5,2,17,16,20,14,13,19,18,15,8,10,7,6,3}); + } + } + + /** + * Concrete implementation of ReflectorB + * Used in Enigma I, M3 + * AY BR CU DH EQ FS GL IP JX KN MO TZ VW + */ + private static class ReflectorB extends Reflector + { + public ReflectorB() + { + super("B", 2, new int[]{24,17,20,7,16,18,11,3,15,23,13,6,14,10,12,8,4,1,5,25,2,22,21,9,0,19}); + } + } + + /** + * Concrete implementation of ReflectorC + * Used in Enigma I, M3 + * AF BV CP DJ EI GO HY KR LZ MX NW QT SU + */ + private static class ReflectorC extends Reflector + { + public ReflectorC() + { + super("C", 3, new int[]{5,21,15,9,8,0,14,24,4,3,17,25,23,22,6,2,19,10,20,16,18,1,13,12,7,11}); + } + } + + /** + * Concrete implementation of thin reflector type b (not equal to normal type b!) + * When used with Rotor Beta on rotation 0, the pair was equivalent to normal reflector B + * S->Beta->ThinB->Beta'->X == X->UKWB->S + * Used in Enigma M4 + * E N K Q A U Y W J I C O P B L M D X Z V F T H R G S + */ + private static class ReflectorThinB extends Reflector + { + public ReflectorThinB() + { + super("ThinB", 4, new int[]{4,13,10,16,0,20,24,22,9,8,2,14,15,1,11,12,3,23,25,21,5,19,7,17,6,18}); + } + } + + /** + * Concrete implementation of thin reflector type c (not equal to normal type c!) + * When used with Rotor Gamma on rotation 0, the pair was equivalent to normal reflector C + * S->Gamma->ThinC->Gamma'->X == X->UKWC->S + * Used in Enigma M4 + * R D O B J N T K V E H M L F C W Z A X G Y I P S U Q + */ + private static class ReflectorThinC extends Reflector + { + public ReflectorThinC() + { + super("ThinC", 5, new int[]{17,3,14,1,9,13,19,10,21,4,7,12,11,5,2,22,25,0,23,6,24,8,15,18,20,16}); + } + } + + /** + * Pluggable Reflector of the Enigma machine of type D and KD + * Standard wiring: AI,BM,CE,DT,FG,HR,JY,KS,LQ,NZ,OX,PW,UV + * Has additional ringSetting and can rotate + */ + public static class ReflectorEnigmaDKD extends Reflector + { + public static final int[] defaultWiring = {8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}; + public ReflectorEnigmaDKD() + { + super("Ref-D", 6, defaultWiring); + } + + public void setConfiguration(int[] conf) + { + this.connections = conf; + } + + public int[] getConfiguration() + { + return this.connections; + } + } + + private static class ReflectorEnigmaK extends Reflector + { + public ReflectorEnigmaK() + { + super("Ref-K", 7, new int[]{8,12,4,19,2,6,5,17,0,24,18,16,1,25,23,22,11,7,10,3,21,20,15,14,9,13}); + } + } + /** + * Reflector as used in the Enigma type T (Tirpitz) + * G E K P B T A U M O C N I L J D X Z Y F H W V Q S R + */ + private static class ReflectorEnigmaT extends Reflector + { + public ReflectorEnigmaT() + { + super("Ref-T", 8, new int[]{6,4,10,15,1,19,0,20,12,14,2,13,8,11,9,3,23,25,24,5,7,22,21,16,18,17}); + } + } + +} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java new file mode 100644 index 0000000..674685f --- /dev/null +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java @@ -0,0 +1,712 @@ +package de.vanitasvitae.enigmandroid.enigma.rotors; + +import android.util.Log; + +/** + * Rotor super class and inner concrete implementations + * The rotors were the key feature of the enigma used to scramble up input signals into + * encrypted signals difficult to predict. The rotors rotated to achieve a poly-alphabetic + * substitution which was hard to break. Each signal passes the rotor twice. Once in "forward"- + * direction and once in "backwards"-direction. There was a set of 3 out of 5 rotors inside the + * enigma machine M4. + * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae + */ +public class Rotor +{ + protected String type; + protected int number; + protected Integer[] connections; + protected Integer[] reversedConnections; + protected Integer[] turnOverNotches; + + protected int ringSetting; + protected int rotation; + + /** + * This constructor is not accessible from outside this class file. + * Use one of the createRotor* factory methods instead to create concrete Rotors. + * Note that connections and reversedConnections MUST be of the same size and that + * neither connections nor reversedConnections respectively MUST have any number between + * 0 and connections.length-1 only once (ie they represent permutations) + * @param type type indicator + * @param connections wiring of the rotor as Integer array + * @param reversedConnections inverse wiring used to encryptString in the opposite direction + * (connections[reversedConnections[i]] = i + * for all i in 0..getRotorSize()-1. + * @param turnOverNotches Position(s) of the turnover notch(es) + * @param ringSetting setting of the ring that holds the letters + * @param rotation rotation of the rotor + */ + protected Rotor(String type, int number, Integer[] connections, Integer[] reversedConnections, + Integer[] turnOverNotches, int ringSetting, int rotation) + { + this.type = type; + this.number = number; + this.connections = connections; + this.reversedConnections = reversedConnections; + this.turnOverNotches = turnOverNotches; + this.ringSetting = ringSetting; + this.rotation = rotation; + } + + /** + * Factory method that creates a rotor accordingly to the type. + * Also initialize the rotor with ringSetting and rotation. + * @param type type indicator (1..10) + * 1..8 -> I..VIII + * 9,10 -> Beta, Gamma + * 11..13 -> DI..DIII + * @param ringSetting setting of the outer ring (0..25) + * @param rotation rotation of the rotor + * @return Concrete rotor + */ + public static Rotor createRotor(int type, int rotation, int ringSetting) + { + switch (type) + { + case 0: return new EntryWheelDK(); + + case 1: return new RotorI(rotation, ringSetting); + case 2: return new RotorII(rotation, ringSetting); + case 3: return new RotorIII(rotation, ringSetting); + case 4: return new RotorIV(rotation, ringSetting); + case 5: return new RotorV(rotation, ringSetting); + case 6: return new RotorVI(rotation, ringSetting); + case 7: return new RotorVII(rotation, ringSetting); + case 8: return new RotorVIII(rotation, ringSetting); + + case 9: return new RotorBeta(rotation, ringSetting); + case 10: return new RotorGamma(rotation, ringSetting); + + case 11: return new RotorDI(rotation, ringSetting); + case 12: return new RotorDII(rotation, ringSetting); + case 13: return new RotorDIII(rotation, ringSetting); + + case 14: return new RotorKI(rotation, ringSetting); + case 15: return new RotorKII(rotation, ringSetting); + case 16: return new RotorKIII(rotation, ringSetting); + + case 17: return new EntryWheelT(); + case 18: return new RotorTI(rotation, ringSetting); + case 19: return new RotorTII(rotation, ringSetting); + case 20: return new RotorTIII(rotation, ringSetting); + case 21: return new RotorTIV(rotation, ringSetting); + case 22: return new RotorTV(rotation, ringSetting); + case 23: return new RotorTVI(rotation, ringSetting); + case 24: return new RotorTVII(rotation, ringSetting); + case 25: return new RotorTVIII(rotation, ringSetting); + + default: return new RotorI(rotation, ringSetting); + } + } + + /** + * Encrypt an input signal via the internal wiring in "forward" direction towards the reflector + * (using connections) + * @param input signal + * @return encrypted signal + */ + public int encryptForward(int input) + { + Log.d(this.getType(),"in "+(char)(input+65)+", out "+(char) (this.connections[input]+65)); + return this.connections[normalize(input)]; + } + + /** + * Encrypt an input signal via the internal wiring in "backwards" direction (using + * reversedConnections) + * @param input signal + * @return encrypted signal + */ + public int encryptBackward(int input) + { + return this.reversedConnections[normalize(input)]; + } + + /** + * Return the type indicator (usually 1..5) + * @return type indicator + */ + public String getType() + { + return this.type; + } + + public int getNumber() + { + return this.number; + } + + /** + * Return the current rotation of the rotor. + * The rotation consists of the actual rotation - the ringSetting + * @return rotation-ringSetting + */ + public int getRotation() + { + return this.rotation; + } + + /** + * Increment rotation of the rotor by one. + */ + public void rotate() + { + this.rotation = normalize(this.getRotation()+1); + } + + /** + * Return true, if the rotor is at a position, where it turns over the next rotor by one + * @return rotation==turnOverNotch + */ + public boolean isAtTurnoverPosition() + { + for(int x : getTurnOverNotches()) + { + //if(x == this.rotation + this.ringSetting) return true; + if(x == this.rotation) return true; + } + return false; + } + + /** + * Return true, if the rotor is in a position where the double turn anomaly happens. + * The double turn anomaly (german: Doppelsprung-Anomalie) is an anomaly in the rotor movement + * caused by the mechanical implementation of the enigma. + * Whenever the rightmost rotor turns the middle rotor AND the middle rotor is only one move + * from turning the leftmost rotor, the middle rotor turns again with the next character. + * So technically there are only 26*25*26 possible rotor settings for any but firmly 3 rotors. + * @return rotation == turnOverNotch-1 + */ + public boolean doubleTurnAnomaly() + { + for(int x : getTurnOverNotches()) + { + if(this.rotation == x-1) return true; + } + return false; + } + + /** + * Returns the positions of the turnover notches in a array + * @return turnOverNotches + */ + public Integer[] getTurnOverNotches() + { + return this.turnOverNotches; + } + + /** + * Return ringSettings of the rotor + * @return ringSetting + */ + public int getRingSetting() + { + return this.ringSetting; + } + + + /** + * Returns the size (ie the number of wires/size of the connections array) + * of the rotor + * @return size + */ + public int getRotorSize() + { + return this.connections.length; + } + + /** + * Normalize the input. + * Normalizing means keeping the input via modulo in the range from 0 to n-1, where n is equal + * to the size of the rotor. This is necessary since java allows negative modulo values, + * which can break this implementation + * @param input input signal + * @return "normalized" input signal + */ + public int normalize(int input) + { + return (input+this.getRotorSize())%this.getRotorSize(); + } + + /** + * Concrete implementation of Rotor of type 1 (I) + * Used in Enigma I, M3, M4 + * E K M F L G D Q V Z N T O W Y H X U S P A I B R C J + */ + private static class RotorI extends Rotor + { + public RotorI(int rotation, int ringSetting) + { + super("I", 1, + new Integer[]{4, 10, 12, 5, 11, 6, 3, 16, 21, 25, 13, 19, 14, 22, 24, 7, 23, 20, 18, 15, 0, 8, 1, 17, 2, 9}, + new Integer[]{20, 22, 24, 6, 0, 3, 5, 15, 21, 25, 1, 4, 2, 10, 12, 19, 7, 23, 18, 11, 17, 8, 13, 16, 14, 9}, + new Integer[]{17}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 2 (II) + * Used in Enigma I, M3, M4 + * A J D K S I R U X B L H W T M C Q G Z N P Y F V O E + */ + private static class RotorII extends Rotor + { + public RotorII(int rotation, int ringSetting) + { + super("II", 2, + new Integer[]{0, 9, 3, 10, 18, 8, 17, 20, 23, 1, 11, 7, 22, 19, 12, 2, 16, 6, 25, 13, 15, 24, 5, 21, 14, 4}, + new Integer[]{0, 9, 15, 2, 25, 22, 17, 11, 5, 1, 3, 10, 14, 19, 24, 20, 16, 6, 4, 13, 7, 23, 12, 8, 21, 18}, + new Integer[]{5}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 3 (III) + * Used in Enigma I, M3, M4 + * B D F H J L C P R T X V Z N Y E I W G A K M U S Q O + */ + private static class RotorIII extends Rotor + { + public RotorIII(int rotation, int ringSetting) + { + super("III", 3, + new Integer[]{1, 3, 5, 7, 9, 11, 2, 15, 17, 19, 23, 21, 25, 13, 24, 4, 8, 22, 6, 0, 10, 12, 20, 18, 16, 14}, + new Integer[]{19, 0, 6, 1, 15, 2, 18, 3, 16, 4, 20, 5, 21, 13, 25, 7, 24, 8, 23, 9, 22, 11, 17, 10, 14, 12}, + new Integer[]{22}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 4 (IV) + * Used in Enigma M3, M4 + * E S O V P Z J A Y Q U I R H X L N F T G K D C M W B + */ + private static class RotorIV extends Rotor + { + public RotorIV(int rotation, int ringSetting) + { + super("IV", 4, + new Integer[]{4, 18, 14, 21, 15, 25, 9, 0, 24, 16, 20, 8, 17, 7, 23, 11, 13, 5, 19, 6, 10, 3, 2, 12, 22, 1}, + new Integer[]{7, 25, 22, 21, 0, 17, 19, 13, 11, 6, 20, 15, 23, 16, 2, 4, 9, 12, 1, 18, 10, 3, 24, 14, 8, 5}, + new Integer[]{10}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 5 (V) + * Used in Enigma M3, M4 + * V Z B R G I T Y U P S D N H L X A W M J Q O F E C K + */ + private static class RotorV extends Rotor + { + public RotorV(int rotation, int ringSetting) + { + super("V", 5, + new Integer[]{21, 25, 1, 17, 6, 8, 19, 24, 20, 15, 18, 3, 13, 7, 11, 23, 0, 22, 12, 9, 16, 14, 5, 4, 2, 10}, + new Integer[]{16, 2, 24, 11, 23, 22, 4, 13, 5, 19, 25, 14, 18, 12, 21, 9, 20, 3, 10, 6, 8, 0, 17, 15, 7, 1}, + new Integer[]{0}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 6 (VI) + * Used in Enigma M3, M4 + * J P G V O U M F Y Q B E N H Z R D K A S X L I C T W + */ + private static class RotorVI extends Rotor + { + public RotorVI(int rotation, int ringSetting) + { + super("VI", 6, + new Integer[]{9,15,6,21,14,20,12,5,24,16,1,4,13,7,25,17,3,10,0,18,23,11,8,2,19,22}, + new Integer[]{18,10,23,16,11,7,2,13,22,0,17,21,6,12,4,1,9,15,19,24,5,3,25,20,8,14}, + new Integer[]{0,13}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 7 (VII) + * Used in Enigma M3, M4 + * N Z J H G R C X M Y S W B O U F A I V L P E K Q D T + */ + private static class RotorVII extends Rotor + { + public RotorVII(int rotation, int ringSetting) + { + super("VII", 7, + new Integer[]{13,25,9,7,6,17,2,23,12,24,18,22,1,14,20,5,0,8,21,11,15,4,10,16,3,19}, + new Integer[]{16,12,6,24,21,15,4,3,17,2,22,19,8,0,13,20,23,5,10,25,14,18,11,7,9,1}, + new Integer[]{0,13}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type 8 (VIII) + * Used in Enigma M3, M4 + * F K Q H T L X O C B J S P D Z R A M E W N I U Y G V + */ + private static class RotorVIII extends Rotor + { + public RotorVIII(int rotation, int ringSetting) + { + super("VIII", 8, + new Integer[]{5,10,16,7,19,11,23,14,2,1,9,18,15,3,25,17,0,12,4,22,13,8,20,24,6,21}, + new Integer[]{16,9,8,13,18,0,24,3,21,10,1,5,17,20,7,12,2,15,11,4,22,25,19,6,23,14}, + new Integer[]{0,13}, ringSetting, rotation); + } + } + + /** + * Concrete implementation of Rotor of type beta (Griechenwalze Beta) + * Beta was used as a "thin" rotor in the M4. It was thinner than a "normal" rotor, so it + * could be used together with one of the two thin reflectors as one rotor. + * When used together with ReflectorThinB, Beta was equivalent to Reflector B (if rotation == 0) + * That way the M4 was backwards compatible to the M3 + * Used in M4 + */ + private static class RotorBeta extends Rotor + { + public RotorBeta(int rotation, int ringSetting) + { + super("Beta", 9, + new Integer[]{11,4,24,9,21,2,13,8,23,22,15,1,16,12,3,17,19,0,10,25,6,5,20,7,14,18}, + new Integer[]{17,11,5,14,1,21,20,23,7,3,18,0,13,6,24,10,12,15,25,16,22,4,9,8,2,19}, + new Integer[]{}, ringSetting, rotation); + } + @Override + public void rotate() + { + //Thin rotors are fixed in position, so they dont rotate + } + + @Override + public boolean doubleTurnAnomaly() + { + //Nope, no anomaly + return false; + } + } + + /** + * Concrete implementation of Rotor of type gamma (Griechenwalze Gamma) + * Gamma was used as a "thin" rotor in the M4. It was thinner than a "normal" rotor, so it + * could be used together with one of the two thin reflectors as one rotor. + * When used together with ReflectorThinC, Gamma is equivalent to Reflector C + * (if rotation == 0). That way the M4 was backwards compatible to the M3 + * Used in M4 + */ + private static class RotorGamma extends Rotor + { + public RotorGamma(int rotation, int ringSetting) + { + super("Gamma", 10, + new Integer[]{5,18,14,10,0,13,20,4,17,7,12,1,19,8,24,2,22,11,16,15,25,23,21,6,9,3}, + new Integer[]{4,11,15,25,7,0,23,9,13,24,3,17,10,5,2,19,18,8,1,12,6,22,16,21,14,20}, + new Integer[]{}, ringSetting, rotation); + } + @Override + public void rotate() + { + //Thin rotors are fixed in position, so they don't rotate + } + + @Override + public boolean doubleTurnAnomaly() + { + //Thin rotors don't do such weird stuff, they're normal just like you and me. + return false; + } + } + + /** + * EntryWheel as used in the Enigma models D, K + * Q W E R T Z U I O A S D F G H J K P Y X C V B N M L + */ + private static class EntryWheelDK extends Rotor + { + public EntryWheelDK() + { + super("ETW-D", 0, + new Integer[]{9,22,20,11,2,12,13,14,7,15,16,25,24,23,8,17,0,3,10,4,6,21,1,19,18,5}, + new Integer[]{16,22,4,17,19,25,20,8,14,0,18,3,5,6,7,9,10,15,24,23,2,21,1,13,12,11}, + new Integer[]{}, 0, 0); + } + @Override + public void rotate() + { + //EntryWheel doesn't rotate + } + + @Override + public boolean doubleTurnAnomaly() + { + //\forall s \in States : nope + return false; + } + } + + /** + * Rotor I as used in the Enigma Type D + * L P G S Z M H A E O Q K V X R F Y B U T N I C J D W + * Turnover Z + */ + private static class RotorDI extends Rotor + { + public RotorDI(int rotation, int ringSetting) + { + super("D-I", 11, + new Integer[]{11,15,6,18,25,12,7,0,4,14,16,10,21,23,17,5,24,1,20,19,13,8,2,9,3,22}, + new Integer[]{7,17,22,24,8,15,2,6,21,23,11,0,5,20,9,1,10,14,3,19,18,12,25,13,16,4}, + new Integer[]{25}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type D + * S L V G B T F X J Q O H E W I R Z Y A M K P C N D U + * Turnover F + */ + private static class RotorDII extends Rotor + { + public RotorDII(int rotation, int ringSetting) + { + super("D-II", 12, + new Integer[]{18,11,21,6,1,19,5,23,9,16,14,7,4,22,8,17,25,24,0,12,10,15,2,13,3,20}, + new Integer[]{18,4,22,24,12,6,3,11,14,8,20,1,19,23,10,21,9,15,0,5,25,2,13,7,17,16}, + new Integer[]{5}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type D + * C J G D P S H K T U R A W Z X F M Y N Q O B V L I E + * Turnover O + */ + private static class RotorDIII extends Rotor + { + public RotorDIII(int rotation, int ringSetting) + { + super("D-III", 13, + new Integer[]{2,9,6,3,15,18,7,10,19,20,17,0,22,25,23,5,12,24,13,16,14,1,21,11,8,4}, + new Integer[]{11,21,0,3,25,15,2,6,24,1,7,23,16,18,20,4,19,10,5,8,9,22,12,14,17,13}, + new Integer[]{14}, ringSetting, rotation); + } + } + + /** + * Rotor I as used in the Enigma Type K (Switzerland) + * P E Z U O H X S C V F M T B G L R I N Q J W A Y D K + * Turnover Z + */ + private static class RotorKI extends Rotor + { + public RotorKI(int rotation, int ringSetting) + { + super("K-I", 14, + new Integer[]{15,4,25,20,14,7,23,18,2,21,5,12,19,1,6,11,17,8,13,16,9,22,0,24,3,10}, + new Integer[]{22,13,8,24,1,10,14,5,17,20,25,15,11,18,4,0,19,16,7,12,3,9,21,6,23,2}, + new Integer[]{25}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type K (Switzerland) + * Z O U E S Y D K F W P C I Q X H M V B L G N J R A T + * Turnover F + */ + private static class RotorKII extends Rotor + { + public RotorKII(int rotation, int ringSetting) + { + super("K-II", 15, + new Integer[]{25,14,20,4,18,24,3,10,5,22,15,2,8,16,23,7,12,21,1,11,6,13,9,17,0,19}, + new Integer[]{24,18,11,6,3,8,20,15,12,22,7,19,16,21,1,10,13,23,4,25,2,17,9,14,5,0}, + new Integer[]{5}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type K (Switzerland) + * E H R V X G A O B Q U S I M Z F L Y N W K T P D J C + * Turnover O + */ + private static class RotorKIII extends Rotor + { + public RotorKIII(int rotation, int ringSetting) + { + super("K-III", 16, + new Integer[]{4,7,17,21,23,6,0,14,1,16,20,18,8,12,25,5,11,24,13,22,10,19,15,3,9,2}, + new Integer[]{6,8,25,23,0,15,5,1,12,24,20,16,13,18,7,22,9,2,11,21,10,3,19,4,17,14}, + new Integer[]{14}, ringSetting, rotation); + } + } + + /** + * EntryWheel as used only in the Enigma Type T Tirpitz + * K Z R O U Q H Y A I G B L W V S T D X F P N M C J E + */ + private static class EntryWheelT extends Rotor + { + public EntryWheelT() + { + super("T-ETW", 17, + new Integer[]{8,11,23,17,25,19,10,6,9,24,0,12,22,21,3,20,5,2,15,16,4,14,13,18,7,1}, + new Integer[]{10,25,17,14,20,16,7,24,0,8,6,1,11,22,21,18,19,3,23,5,15,13,12,2,9,4}, + new Integer[]{}, 0, 0); + } + @Override + public void rotate() + { + //EntryWheel doesn't rotate + } + + @Override + public boolean doubleTurnAnomaly() + { + //\forall s \in States : nope + return false; + } + } + /** + * Rotor I as used in the Enigma Type T Tirpitz + * K P T Y U E L O C V G R F Q D A N J M B S W H Z X I + * Turnover X A F L R + */ + private static class RotorTI extends Rotor + { + public RotorTI(int rotation, int ringSetting) + { + super("T-I", 18, + new Integer[]{10,15,19,24,20,4,11,14,2,21,6,17,5,16,3,0,13,9,12,1,18,22,7,25,23,8}, + new Integer[]{15,19,8,14,5,12,10,22,25,17,0,6,18,16,7,1,13,11,20,2,4,9,21,24,3,23}, + new Integer[]{23,0,5,11,17}, ringSetting, rotation); + } + } + + /** + * Rotor II as used in the Enigma Type T Tirpitz + * U P H Z L W E Q M T D J X C A K S O I G V B Y F N R + * Turnover X A G M S + */ + private static class RotorTII extends Rotor + { + public RotorTII(int rotation, int ringSetting) + { + super("T-II", 19, + new Integer[]{20,15,7,25,11,22,4,16,12,19,3,9,23,2,0,10,18,14,8,6,21,1,24,5,13,17}, + new Integer[]{14,21,13,10,6,23,19,2,18,11,15,4,8,24,17,1,7,25,16,9,0,20,5,12,22,3}, + new Integer[]{23,0,6,12,18}, ringSetting, rotation); + } + } + + /** + * Rotor III as used in the Enigma Type T Tirpitz + * Q U D L Y R F E K O N V Z A X W H M G P J B S I C T + * Turnover X A F L R + */ + private static class RotorTIII extends Rotor + { + public RotorTIII(int rotation, int ringSetting) { + super("T-III", 20, + new Integer[]{16,20,3,11,24,17,5,4,10,14,13,21,25,0,23,22,7,12,6,15,9,1,18,8,2,19}, + new Integer[]{13,21,24,2,7,6,18,16,23,20,8,3,17,10,9,19,0,5,22,25,1,11,15,14,4,12}, + new Integer[]{23,0,5,11,17}, ringSetting, rotation); + } + } + + /** + * Rotor IV as used in the Enigma Type T Tirpitz + * C I W T B K X N R E S P F L Y D A G V H Q U O J Z M + * Turnover X A G M S + */ + private static class RotorTIV extends Rotor + { + public RotorTIV(int rotation, int ringSetting) + { + super("T-IV", 21, + new Integer[]{2,8,22,19,1,10,23,13,17,4,18,15,5,11,24,3,0,6,21,7,16,20,14,9,25,12}, + new Integer[]{16,4,0,15,9,12,17,19,1,23,5,13,25,7,22,11,20,8,10,3,21,18,2,6,14,24}, + new Integer[]{23,0,6,12,18}, ringSetting, rotation); + } + } + + /** + * Rotor V as used in the Enigma Type T Tirpitz + * U A X G I S N J B V E R D Y L F Z W T P C K O H M Q + * Turnover Z D G L S + */ + private static class RotorTV extends Rotor + { + public RotorTV(int rotation, int ringSetting) + { + super("T-V", 22, + new Integer[]{20,0,23,6,8,18,13,9,1,21,4,17,3,24,11,5,25,22,19,15,2,10,14,7,12,16}, + new Integer[]{1,8,20,12,10,15,3,23,4,7,21,14,24,6,22,19,25,11,5,18,0,9,17,2,13,16}, + new Integer[]{25,3,6,11,18}, ringSetting, rotation); + } + } + + /** + * Rotor VI as used in the Enigma Type T Tirpitz + * X F U Z G A L V H C N Y S E W Q T D M R B K P I O J + * Turnover Y F J N R + */ + private static class RotorTVI extends Rotor + { + public RotorTVI(int rotation, int ringSetting) + { + super("T-VI", 23, + new Integer[]{23,5,20,25,6,0,11,21,7,2,13,24,18,4,22,16,19,3,12,17,1,10,15,8,14,9}, + new Integer[]{5,20,9,17,13,1,4,8,23,25,21,6,18,10,24,22,15,19,12,16,2,7,14,0,11,3}, + new Integer[]{24,5,9,13,17}, ringSetting, rotation); + } + } + + /** + * Rotor VII as used in the Enigma Type T Tirpitz + * B J V F T X P L N A Y O Z I K W G D Q E R U C H S M + * Turnover Z D G L S + */ + private static class RotorTVII extends Rotor + { + public RotorTVII(int rotation, int ringSetting) + { + super("T-VII", 24, + new Integer[]{1,9,21,5,19,23,15,11,13,0,24,14,25,8,10,22,6,3,16,4,17,20,2,7,18,12}, + new Integer[]{9,0,22,17,19,3,16,23,13,1,14,7,25,8,11,6,18,20,24,4,21,2,15,5,10,12}, + new Integer[]{25,3,6,11,18}, ringSetting, rotation); + } + } + + /** + * Rotor VIII as used in the Enigma Type T Tirpitz + * Y M T P N Z H W K O D A J X E L U Q V G C B I S F R + * Turnover Y F J N R + */ + private static class RotorTVIII extends Rotor + { + public RotorTVIII(int rotation, int ringSetting) + { + super("T-VIII", 25, + new Integer[]{24,12,19,15,13,25,7,22,10,14,3,0,9,23,4,11,20,16,21,6,2,1,8,18,5,17}, + new Integer[]{11,21,20,10,14,24,19,6,22,12,8,15,1,4,9,3,17,25,23,2,16,18,7,13,0,5}, + new Integer[]{24,5,9,13,17}, ringSetting, rotation); + } + } +} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java index 1d7d484..9e7bb36 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java @@ -1,26 +1,9 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; -import android.widget.ArrayAdapter; import android.widget.EditText; -import android.widget.Spinner; import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.SettingsActivity; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.inputPreparer.EditTextAdapter; @@ -30,163 +13,129 @@ import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer; * Abstract LayoutContainer for Enigma machines * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public abstract class LayoutContainer { - final EditText inputView; - private final EditText outputView; + protected EditText inputView; + protected EditText outputView; - EditTextAdapter input; - EditTextAdapter output; + protected EditTextAdapter input; + protected EditTextAdapter output; - InputPreparer inputPreparer; - final MainActivity main; + protected InputPreparer inputPreparer; + protected MainActivity main; - public abstract Enigma getEnigma(); - protected abstract void assembleLayout(); - public abstract void resetLayout(); - protected abstract void setLayoutState(EnigmaStateBundle state); - public abstract void syncStateFromLayoutToEnigma(); - public void syncStateFromEnigmaToLayout() - { - this.setLayoutState(getEnigma().getState()); - } - public abstract void showRingSettingsDialog(); + protected EnigmaStateBundle state; - LayoutContainer() - { - main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - setEnigmaLayout(); - this.inputView = (EditText) main.findViewById(R.id.input); - this.outputView = (EditText) main.findViewById(R.id.output); - input = EditTextAdapter.createEditTextAdapter(inputView, - SettingsActivity.SettingsSingleton.getInstance().getPrefMessageFormatting()); - output = EditTextAdapter.createEditTextAdapter(outputView, - SettingsActivity.SettingsSingleton.getInstance().getPrefMessageFormatting()); - inputPreparer = InputPreparer.createInputPreparer(); - assembleLayout(); - finishLayout(); - } + public abstract Enigma getEnigma(); + protected abstract void initializeLayout(); + public abstract void resetLayout(); + protected abstract void setLayoutState(EnigmaStateBundle state); + protected abstract void refreshState(); + public abstract void showRingSettingsDialog(); - public void doCrypto() - { - if(inputView.getText().length()!=0) - { - syncStateFromLayoutToEnigma(); - String message = inputView.getText().toString(); - message = inputPreparer.prepareString(message); - input.setText(message); - output.setText(getEnigma().encryptString(message)); - setLayoutState(getEnigma().getState()); - } - } + public LayoutContainer(EnigmaStateBundle state) + { + main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); + this.inputView = (EditText) main.findViewById(R.id.input); + this.outputView = (EditText) main.findViewById(R.id.output); + input = EditTextAdapter.createEditTextAdapter(inputView, main.getPrefMessageFormatting()); + output = EditTextAdapter.createEditTextAdapter(outputView, main.getPrefMessageFormatting()); + initializeLayout(); + this.state = state; + } + public LayoutContainer() + { + main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); + this.inputView = (EditText) main.findViewById(R.id.input); + this.outputView = (EditText) main.findViewById(R.id.output); + input = EditTextAdapter.createEditTextAdapter(inputView, main.getPrefMessageFormatting()); + output = EditTextAdapter.createEditTextAdapter(outputView, main.getPrefMessageFormatting()); + initializeLayout(); + } - public EditTextAdapter getInput() - { - return this.input; - } + public void doCrypto() + { + if(inputView.getText().length()!=0) + { + getEnigma().setState(getState()); + String message = inputView.getText().toString(); + message = inputPreparer.prepareString(message); + input.setText(message); + output.setText(getEnigma().encryptString(message)); + setLayoutState(getEnigma().getState()); + } + } - public EditTextAdapter getOutput() - { - return this.output; - } + public EnigmaStateBundle getState() + { + refreshState(); + return state; + } - public static LayoutContainer createLayoutContainer() - { - return createLayoutContainer(SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType()); - } + public void setState(EnigmaStateBundle state) + { + this.state = state; + } - private static LayoutContainer createLayoutContainer(String enigmaType) - { - switch (enigmaType) { - case "I": - return new LayoutContainer_I(); - case "M3": - return new LayoutContainer_M3(); - case "M4": - return new LayoutContainer_M4(); - case "D": - return new LayoutContainer_D(); - case "K": - return new LayoutContainer_K(); - case "KS": - return new LayoutContainer_K_Swiss(); - case "KSA": - return new LayoutContainer_K_Swiss_Airforce(); - case "T": - return new LayoutContainer_T(); - case "R": - return new LayoutContainer_R(); - case "G31": - return new LayoutContainer_G31(); - case "G312": - return new LayoutContainer_G312(); - case "G260": - return new LayoutContainer_G260(); - case "KD": - return new LayoutContainer_KD(); - default: - return new LayoutContainer_I(); - } - } + public EditTextAdapter getInput() + { + return this.input; + } - /** - * Add ArrayAdapter, contents and layouts to Spinner - * @param view Spinner - * @param resourceID ID of the referenced array (eg. R.array.rotor_1_8) - */ - void prepareSpinnerAdapter(Spinner view, int resourceID) { - MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); + public EditTextAdapter getOutput() + { + return this.output; + } - ArrayAdapter ad = new ArrayAdapter(main, - android.R.layout.simple_spinner_item, - main.getResources().getTextArray(resourceID)); + public static LayoutContainer createLayoutContainer(String enigmaType) + { + switch (enigmaType) { + case "I": + return new LayoutContainer_I(); + case "M3": + return new LayoutContainer_M3(); + case "M4": + return new LayoutContainer_M4(); + case "D": + return new LayoutContainer_D(); + case "K": + return new LayoutContainer_K(); + case "T": + return new LayoutContainer_T(); + default: + return new LayoutContainer_I(); + } + } - ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - ad.setDropDownViewTheme(main.getTheme()); - view.setAdapter(ad); - } + public void setInputPreparer(InputPreparer inputPreparer) + { + this.inputPreparer = inputPreparer; + } - /** - * Add ArrayAdapter, contents and layouts to Spinner - * @param view Spinner - * @param array Character array - */ - void prepareSpinnerAdapter(Spinner view, Character[] array) - { - MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - ArrayAdapter adapter = new ArrayAdapter<>(main, - android.R.layout.simple_spinner_item, array); - adapter.setDropDownViewResource( - android.R.layout.simple_spinner_dropdown_item); - adapter.setDropDownViewTheme(main.getTheme()); - view.setAdapter(adapter); - } + public void setEditTextAdapter(String type) + { + String in = input.getText(); + String out = output.getText(); + input = EditTextAdapter.createEditTextAdapter(inputView, type); + input.setText(in); + output = EditTextAdapter.createEditTextAdapter(outputView, type); + output.setText(out); + } +} - public void setInputPreparer(InputPreparer inputPreparer) - { - this.inputPreparer = inputPreparer; - } - - public void setEditTextAdapter(String type) - { - String in = input.getText(); - String out = output.getText(); - input = EditTextAdapter.createEditTextAdapter(inputView, type); - input.setText(in); - output = EditTextAdapter.createEditTextAdapter(outputView, type); - output.setText(out); - } - - protected void setMainActivityLayout() - { - setEnigmaLayout(); - } - - abstract protected void setEnigmaLayout(); - - private void finishLayout() - { - //TODO - } -} \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java index e7849bb..6272270 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_D.java @@ -1,41 +1,42 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; -import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_D; /** - * Concrete LayoutContainer for the D layout. + * Concrete LayoutContainer for the M3 layout. * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class LayoutContainer_D extends LayoutContainer { private Enigma_D enigma; - private Spinner rotor1PositionView; - private Spinner rotor2PositionView; - private Spinner rotor3PositionView; - private Spinner reflectorPositionView; + protected Spinner rotor1PositionView; + protected Spinner rotor2PositionView; + protected Spinner rotor3PositionView; + protected Spinner reflectorPositionView; public LayoutContainer_D() { @@ -45,13 +46,7 @@ public class LayoutContainer_D extends LayoutContainer } @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_d); - } - - @Override - protected void assembleLayout() + protected void initializeLayout() { this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); @@ -61,17 +56,33 @@ public class LayoutContainer_D extends LayoutContainer reflectorWiring.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - new PluggableDialogBuilder(getEnigma().getState()).showDialogReflector(); + new PluggableDialogBuilder(state).showDialogReflector(); } }); Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /**Fill with A..Z*/} - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); + ArrayAdapter rotor1PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor1PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor1PositionView.setAdapter(rotor1PositionAdapter); + ArrayAdapter rotor2PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor2PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor2PositionView.setAdapter(rotor2PositionAdapter); + ArrayAdapter rotor3PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor3PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor3PositionView.setAdapter(rotor3PositionAdapter); + ArrayAdapter reflectorPositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + reflectorPositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + reflectorPositionView.setAdapter(reflectorPositionAdapter); } @Override @@ -84,8 +95,9 @@ public class LayoutContainer_D extends LayoutContainer } @Override - public void setLayoutState(EnigmaStateBundle state) + protected void setLayoutState(EnigmaStateBundle state) { + this.state = state; this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -93,14 +105,12 @@ public class LayoutContainer_D extends LayoutContainer } @Override - public void syncStateFromLayoutToEnigma() + protected void refreshState() { - EnigmaStateBundle state = getEnigma().getState(); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); } public Enigma_D getEnigma() @@ -112,6 +122,6 @@ public class LayoutContainer_D extends LayoutContainer public void showRingSettingsDialog() { new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); + createRingSettingsDialog(state); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java deleted file mode 100644 index 822a55b..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import de.vanitasvitae.enigmandroid.enigma.Enigma_G260; - -/** - * LayoutContainer for the Enigma Model G260 - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_G260 extends LayoutContainer_G31 -{ - public LayoutContainer_G260() - { - super(); - main.setTitle("G260 - EnigmAndroid"); - this.resetLayout(); - } - - @Override - public void resetLayout() { - enigma = new Enigma_G260(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java deleted file mode 100644 index ba5f726..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java +++ /dev/null @@ -1,123 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import android.widget.Spinner; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; -import de.vanitasvitae.enigmandroid.enigma.Enigma_G31; - -/** - * LayoutContainer for the Enigma Model G31 - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_G31 extends LayoutContainer -{ - Enigma enigma; - - private Spinner rotor1View; - private Spinner rotor2View; - private Spinner rotor3View; - - private Spinner rotor1PositionView; - private Spinner rotor2PositionView; - private Spinner rotor3PositionView; - private Spinner reflectorPositionView; - - public LayoutContainer_G31() - { - super(); - main.setTitle("G31 - EnigmAndroid"); - this.resetLayout(); - } - - @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t); - } - - @Override - public Enigma getEnigma() { - return this.enigma; - } - - @Override - protected void assembleLayout() { - this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); - this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); - this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); - this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); - this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); - this.rotor3PositionView = (Spinner) main.findViewById(R.id.rotor3position); - this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); - - Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} - - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); - } - - @Override - public void resetLayout() { - enigma = new Enigma_G31(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } - - @Override - public void setLayoutState(EnigmaStateBundle state) - { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); - this.rotor1PositionView.setSelection(state.getRotationRotor1()); - this.rotor2PositionView.setSelection(state.getRotationRotor2()); - this.rotor3PositionView.setSelection(state.getRotationRotor3()); - this.reflectorPositionView.setSelection(state.getRotationReflector()); - } - - @Override - public void syncStateFromLayoutToEnigma() - { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); - state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); - state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); - state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); - } - - @Override - public void showRingSettingsDialog() - { - new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java deleted file mode 100644 index e83e8f7..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G312.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import de.vanitasvitae.enigmandroid.enigma.Enigma_G312; - -/** - * LayoutContainer for the Enigma Model G312 - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_G312 extends LayoutContainer_G31 -{ - public LayoutContainer_G312() - { - super(); - main.setTitle("G312 - EnigmAndroid"); - this.resetLayout(); - } - - @Override - public void resetLayout() { - enigma = new Enigma_G312(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java index d0edad8..80902f9 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java @@ -1,24 +1,10 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; -import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_I; @@ -27,18 +13,33 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_I; * Concrete LayoutContainer for the Enigma I layout. * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class LayoutContainer_I extends LayoutContainer { private Enigma_I enigma; - Spinner rotor1View; - Spinner rotor2View; - Spinner rotor3View; - Spinner reflectorView; - Spinner rotor1PositionView; - Spinner rotor2PositionView; - Spinner rotor3PositionView; + protected Spinner rotor1View; + protected Spinner rotor2View; + protected Spinner rotor3View; + protected Spinner reflectorView; + protected Spinner rotor1PositionView; + protected Spinner rotor2PositionView; + protected Spinner rotor3PositionView; public LayoutContainer_I() { @@ -48,13 +49,7 @@ public class LayoutContainer_I extends LayoutContainer } @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_i_m3); - } - - @Override - protected void assembleLayout() + protected void initializeLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); @@ -67,20 +62,46 @@ public class LayoutContainer_I extends LayoutContainer setPlugboardButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - new PluggableDialogBuilder(getEnigma().getState()).showDialogPlugboard(); + new PluggableDialogBuilder(state).showDialogPlugboard(); } }); Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /**Fill with A..Z*/} - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_5); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_5); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_5); - prepareSpinnerAdapter(reflectorView, R.array.reflectors_a_c); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); + ArrayAdapter rotor1Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_5, + android.R.layout.simple_spinner_item); + rotor1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor1View.setAdapter(rotor1Adapter); + ArrayAdapter rotor2Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_5, + android.R.layout.simple_spinner_item); + rotor2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor2View.setAdapter(rotor2Adapter); + ArrayAdapter rotor3Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_5, + android.R.layout.simple_spinner_item); + rotor3Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor3View.setAdapter(rotor3Adapter); + + ArrayAdapter reflectorAdapter = ArrayAdapter.createFromResource(main, R.array.reflectors_a_c, + android.R.layout.simple_spinner_item); + reflectorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + reflectorView.setAdapter(reflectorAdapter); + + ArrayAdapter rotor1PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor1PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor1PositionView.setAdapter(rotor1PositionAdapter); + ArrayAdapter rotor2PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor2PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor2PositionView.setAdapter(rotor2PositionAdapter); + ArrayAdapter rotor3PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor3PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor3PositionView.setAdapter(rotor3PositionAdapter); } @Override @@ -93,29 +114,28 @@ public class LayoutContainer_I extends LayoutContainer } @Override - public void setLayoutState(EnigmaStateBundle state) + protected void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); - this.reflectorView.setSelection(state.getTypeReflector()); + this.state = state; + this.rotor1View.setSelection(state.getTypeRotor1() - 1); + this.rotor2View.setSelection(state.getTypeRotor2() - 1); + this.rotor3View.setSelection(state.getTypeRotor3() - 1); + this.reflectorView.setSelection(state.getTypeReflector() - 1); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); } @Override - public void syncStateFromLayoutToEnigma() + protected void refreshState() { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setTypeReflector(reflectorView.getSelectedItemPosition()); + state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 1); + state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 1); + state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 1); + state.setTypeReflector(reflectorView.getSelectedItemPosition() + 1); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); - getEnigma().setState(state); } public Enigma_I getEnigma() @@ -127,6 +147,6 @@ public class LayoutContainer_I extends LayoutContainer public void showRingSettingsDialog() { new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRot(). - createRingSettingsDialog(getEnigma().getState()); + createRingSettingsDialog(state); } } \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java index 14fb887..b3fc225 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K.java @@ -1,22 +1,8 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; +import android.widget.ArrayAdapter; import android.widget.Spinner; -import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; @@ -26,19 +12,34 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_K; * LayoutContainer for the Enigma Model K * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class LayoutContainer_K extends LayoutContainer { - Enigma enigma; + private Enigma_K enigma; - private Spinner rotor1View; - private Spinner rotor2View; - private Spinner rotor3View; + protected Spinner rotor1View; + protected Spinner rotor2View; + protected Spinner rotor3View; - private Spinner rotor1PositionView; - private Spinner rotor2PositionView; - private Spinner rotor3PositionView; - private Spinner reflectorPositionView; + protected Spinner rotor1PositionView; + protected Spinner rotor2PositionView; + protected Spinner rotor3PositionView; + protected Spinner reflectorPositionView; public LayoutContainer_K() { @@ -47,19 +48,13 @@ public class LayoutContainer_K extends LayoutContainer this.resetLayout(); } - @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t); - } - @Override public Enigma getEnigma() { return this.enigma; } @Override - protected void assembleLayout() { + protected void initializeLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); @@ -69,16 +64,42 @@ public class LayoutContainer_K extends LayoutContainer this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /**Fill with A..Z*/} - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); + ArrayAdapter rotor1Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_3, + android.R.layout.simple_spinner_item); + rotor1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor1View.setAdapter(rotor1Adapter); + ArrayAdapter rotor2Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_3, + android.R.layout.simple_spinner_item); + rotor2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor2View.setAdapter(rotor2Adapter); + ArrayAdapter rotor3Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_3, + android.R.layout.simple_spinner_item); + rotor3Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor3View.setAdapter(rotor3Adapter); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); + ArrayAdapter rotor1PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + rotor1PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor1PositionView.setAdapter(rotor1PositionAdapter); + ArrayAdapter rotor2PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + rotor2PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor2PositionView.setAdapter(rotor2PositionAdapter); + ArrayAdapter rotor3PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + rotor3PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor3PositionView.setAdapter(rotor3PositionAdapter); + + ArrayAdapter reflectorPositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + reflectorPositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + reflectorPositionView.setAdapter(reflectorPositionAdapter); } @Override @@ -90,11 +111,12 @@ public class LayoutContainer_K extends LayoutContainer } @Override - public void setLayoutState(EnigmaStateBundle state) + protected void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); + this.state = state; + this.rotor1View.setSelection(state.getTypeRotor1()-14); + this.rotor2View.setSelection(state.getTypeRotor2()-14); + this.rotor3View.setSelection(state.getTypeRotor3()-14); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -102,23 +124,21 @@ public class LayoutContainer_K extends LayoutContainer } @Override - public void syncStateFromLayoutToEnigma() + protected void refreshState() { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 14); + state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 14); + state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 14); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); } @Override public void showRingSettingsDialog() { new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); + createRingSettingsDialog(state); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java deleted file mode 100644 index 9a35a50..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java +++ /dev/null @@ -1,133 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import android.view.View; -import android.widget.Button; -import android.widget.Spinner; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; -import de.vanitasvitae.enigmandroid.enigma.Enigma_KD; - -/** - * LayoutContainer for the Enigma Model K - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_KD extends LayoutContainer -{ - private Enigma enigma; - - private Spinner rotor1View; - private Spinner rotor2View; - private Spinner rotor3View; - - private Spinner rotor1PositionView; - private Spinner rotor2PositionView; - private Spinner rotor3PositionView; - private Spinner reflectorPositionView; - - public LayoutContainer_KD() - { - super(); - main.setTitle("KD - EnigmAndroid"); - this.resetLayout(); - } - - @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_kd); - } - - @Override - public Enigma getEnigma() { - return this.enigma; - } - - @Override - protected void assembleLayout() { - this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); - this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); - this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); - this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); - this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); - this.rotor3PositionView = (Spinner) main.findViewById(R.id.rotor3position); - this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); - Button reflectorWiring = (Button) main.findViewById(R.id.button_reflector); - reflectorWiring.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - new PluggableDialogBuilder(getEnigma().getState()).showDialogReflector(); - } - }); - - Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} - - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); - - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); - } - - @Override - public void resetLayout() { - enigma = new Enigma_KD(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } - - @Override - public void setLayoutState(EnigmaStateBundle state) - { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); - this.rotor1PositionView.setSelection(state.getRotationRotor1()); - this.rotor2PositionView.setSelection(state.getRotationRotor2()); - this.rotor3PositionView.setSelection(state.getRotationRotor3()); - this.reflectorPositionView.setSelection(state.getRotationReflector()); - } - - @Override - public void syncStateFromLayoutToEnigma() - { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); - state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); - state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); - state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); - } - - @Override - public void showRingSettingsDialog() - { - new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java deleted file mode 100644 index 5d76df1..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Standard; - -/** - * LayoutContainer for the Enigma Model K - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_K_Swiss extends LayoutContainer_K -{ - public LayoutContainer_K_Swiss() - { - super(); - main.setTitle("KS - EnigmAndroid"); - this.resetLayout(); - } - - @Override - public void resetLayout() { - enigma = new Enigma_K_Swiss_Standard(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java deleted file mode 100644 index c0e7c80..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Airforce; - -/** - * LayoutContainer for the Enigma Model K - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer_K -{ - public LayoutContainer_K_Swiss_Airforce() - { - super(); - main.setTitle("KSA - EnigmAndroid"); - this.resetLayout(); - } - - @Override - public void resetLayout() { - enigma = new Enigma_K_Swiss_Airforce(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java index ed580a2..999dfd8 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java @@ -1,20 +1,7 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; @@ -26,6 +13,21 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_M3; * Concrete LayoutContainer for the M3 layout. * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class LayoutContainer_M3 extends LayoutContainer_I { @@ -39,7 +41,7 @@ public class LayoutContainer_M3 extends LayoutContainer_I } @Override - protected void assembleLayout() + protected void initializeLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); @@ -52,20 +54,46 @@ public class LayoutContainer_M3 extends LayoutContainer_I setPlugboardButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - new PluggableDialogBuilder(getEnigma().getState()).showDialogPlugboard(); + new PluggableDialogBuilder(state).showDialogPlugboard(); } }); Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /**Fill with A..Z*/} - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_8); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_8); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_8); - prepareSpinnerAdapter(reflectorView, R.array.reflectors_b_c); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); + ArrayAdapter rotor1Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor1View.setAdapter(rotor1Adapter); + ArrayAdapter rotor2Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor2View.setAdapter(rotor2Adapter); + ArrayAdapter rotor3Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor3Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor3View.setAdapter(rotor3Adapter); + + ArrayAdapter reflectorAdapter = ArrayAdapter.createFromResource(main, R.array.reflectors_b_c, + android.R.layout.simple_spinner_item); + reflectorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + reflectorView.setAdapter(reflectorAdapter); + + ArrayAdapter rotor1PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor1PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor1PositionView.setAdapter(rotor1PositionAdapter); + ArrayAdapter rotor2PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor2PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor2PositionView.setAdapter(rotor2PositionAdapter); + ArrayAdapter rotor3PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor3PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor3PositionView.setAdapter(rotor3PositionAdapter); } @Override @@ -78,29 +106,28 @@ public class LayoutContainer_M3 extends LayoutContainer_I } @Override - public void setLayoutState(EnigmaStateBundle state) + protected void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); - this.reflectorView.setSelection(state.getTypeReflector()); + this.state = state; + this.rotor1View.setSelection(state.getTypeRotor1()-1); + this.rotor2View.setSelection(state.getTypeRotor2() - 1); + this.rotor3View.setSelection(state.getTypeRotor3() - 1); + this.reflectorView.setSelection(state.getTypeReflector() - 2); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); } @Override - public void syncStateFromLayoutToEnigma() + protected void refreshState() { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setTypeReflector(reflectorView.getSelectedItemPosition()); + state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 1); + state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 1); + state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 1); + state.setTypeReflector(reflectorView.getSelectedItemPosition() + 2); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); - getEnigma().setState(state); } @Override diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java index 58ac2c6..3c8ab1e 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M4.java @@ -1,24 +1,10 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; -import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_M4; @@ -27,6 +13,21 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_M4; * Concrete LayoutContainer for the M4 layout. * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class LayoutContainer_M4 extends LayoutContainer { @@ -49,11 +50,6 @@ public class LayoutContainer_M4 extends LayoutContainer this.resetLayout(); } - @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_m4); - } public Enigma_M4 getEnigma() @@ -62,7 +58,7 @@ public class LayoutContainer_M4 extends LayoutContainer } @Override - protected void assembleLayout() { + protected void initializeLayout() { this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); @@ -73,25 +69,65 @@ public class LayoutContainer_M4 extends LayoutContainer this.rotor4PositionView = (Spinner) main.findViewById(R.id.thin_rotor_position); this.reflectorView = (Spinner) main.findViewById(R.id.reflector); Button setPlugboardButton = (Button) main.findViewById(R.id.button_plugboard); - if(setPlugboardButton != null) setPlugboardButton.setOnClickListener(new View.OnClickListener() { + setPlugboardButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - new PluggableDialogBuilder(getEnigma().getState()).showDialogPlugboard(); + new PluggableDialogBuilder(state).showDialogPlugboard(); } }); Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} + for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /**Fill with A..Z*/} - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_8); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_8); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_8); - prepareSpinnerAdapter(rotor4View, R.array.rotors_beta_gamma); - prepareSpinnerAdapter(reflectorView, R.array.reflectors_b_c); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor4PositionView, rotorPositionArray); + ArrayAdapter rotor1Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor1View.setAdapter(rotor1Adapter); + + ArrayAdapter rotor2Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor2View.setAdapter(rotor2Adapter); + + ArrayAdapter rotor3Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor3Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor3View.setAdapter(rotor3Adapter); + + ArrayAdapter rotor4Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_beta_gamma, + android.R.layout.simple_spinner_item); + rotor4Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor4View.setAdapter(rotor4Adapter); + + + ArrayAdapter reflectorAdapter = ArrayAdapter.createFromResource(main, R.array.reflectors_b_c, + android.R.layout.simple_spinner_item); + reflectorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + reflectorView.setAdapter(reflectorAdapter); + + ArrayAdapter rotor1PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor1PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor1PositionView.setAdapter(rotor1PositionAdapter); + + ArrayAdapter rotor2PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor2PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor2PositionView.setAdapter(rotor2PositionAdapter); + + ArrayAdapter rotor3PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor3PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor3PositionView.setAdapter(rotor3PositionAdapter); + + ArrayAdapter rotor4PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item,rotorPositionArray); + rotor4PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor4PositionView.setAdapter(rotor4PositionAdapter); } @Override @@ -103,12 +139,13 @@ public class LayoutContainer_M4 extends LayoutContainer } @Override - public void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); - this.rotor4View.setSelection(state.getTypeRotor4()); - this.reflectorView.setSelection(state.getTypeReflector()); + protected void setLayoutState(EnigmaStateBundle state) { + this.state = state; + this.rotor1View.setSelection(state.getTypeRotor1()-1); + this.rotor2View.setSelection(state.getTypeRotor2() - 1); + this.rotor3View.setSelection(state.getTypeRotor3() - 1); + this.rotor4View.setSelection(state.getTypeRotor4() - 9); + this.reflectorView.setSelection(state.getTypeReflector() - 4); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); this.rotor3PositionView.setSelection(state.getRotationRotor3()); @@ -116,24 +153,22 @@ public class LayoutContainer_M4 extends LayoutContainer } @Override - public void syncStateFromLayoutToEnigma() { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setTypeRotor4(rotor4View.getSelectedItemPosition()); - state.setTypeReflector(reflectorView.getSelectedItemPosition()); + protected void refreshState() { + state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 1); + state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 1); + state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 1); + state.setTypeRotor4(rotor4View.getSelectedItemPosition() + 9); + state.setTypeReflector(reflectorView.getSelectedItemPosition() + 4); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); state.setRotationRotor4(rotor4PositionView.getSelectedItemPosition()); - getEnigma().setState(state); } @Override public void showRingSettingsDialog() { new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRot(). - createRingSettingsDialog(getEnigma().getState()); + createRingSettingsDialog(state); } } \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java deleted file mode 100644 index c2f73f2..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java +++ /dev/null @@ -1,123 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import android.widget.Spinner; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.R; -import de.vanitasvitae.enigmandroid.enigma.Enigma; -import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; -import de.vanitasvitae.enigmandroid.enigma.Enigma_R; - -/** - * LayoutContainer for the Enigma Model R - * This class contains the layout and controls the layout elements such as spinners and stuff - * Copyright (C) 2015 Paul Schaub - */ -public class LayoutContainer_R extends LayoutContainer -{ - private Enigma_R enigma; - - private Spinner rotor1View; - private Spinner rotor2View; - private Spinner rotor3View; - - private Spinner rotor1PositionView; - private Spinner rotor2PositionView; - private Spinner rotor3PositionView; - private Spinner reflectorPositionView; - - public LayoutContainer_R() - { - super(); - main.setTitle("R - EnigmAndroid"); - this.resetLayout(); - } - - @Override - protected void setEnigmaLayout() - { - MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t); - } - - @Override - public Enigma getEnigma() { - return this.enigma; - } - - @Override - protected void assembleLayout() { - this.rotor1View = (Spinner) main.findViewById(R.id.rotor1); - this.rotor2View = (Spinner) main.findViewById(R.id.rotor2); - this.rotor3View = (Spinner) main.findViewById(R.id.rotor3); - this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position); - this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position); - this.rotor3PositionView = (Spinner) main.findViewById(R.id.rotor3position); - this.reflectorPositionView = (Spinner) main.findViewById(R.id.reflector_position); - - Character[] rotorPositionArray = new Character[26]; - for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/} - - prepareSpinnerAdapter(rotor1View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor2View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor3View, R.array.rotors_1_3); - prepareSpinnerAdapter(rotor1PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor2PositionView, rotorPositionArray); - prepareSpinnerAdapter(rotor3PositionView, rotorPositionArray); - prepareSpinnerAdapter(reflectorPositionView, rotorPositionArray); - } - - @Override - public void resetLayout() { - enigma = new Enigma_R(); - setLayoutState(enigma.getState()); - output.setText(""); - input.setText(""); - } - - @Override - public void setLayoutState(EnigmaStateBundle state) - { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); - this.rotor1PositionView.setSelection(state.getRotationRotor1()); - this.rotor2PositionView.setSelection(state.getRotationRotor2()); - this.rotor3PositionView.setSelection(state.getRotationRotor3()); - this.reflectorPositionView.setSelection(state.getRotationReflector()); - } - - @Override - public void syncStateFromLayoutToEnigma() - { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); - state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); - state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); - state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); - state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); - } - - @Override - public void showRingSettingsDialog() - { - new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); - } -} diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java index 82cc3a8..7eb33f6 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_T.java @@ -1,44 +1,45 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; +import android.widget.ArrayAdapter; import android.widget.Spinner; -import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.Enigma_T; /** - * LayoutContainer for the Enigma Model T + * LayoutContainer for the Enigma Model K * This class contains the layout and controls the layout elements such as spinners and stuff * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ public class LayoutContainer_T extends LayoutContainer { private Enigma_T enigma; - private Spinner rotor1View; - private Spinner rotor2View; - private Spinner rotor3View; + protected Spinner rotor1View; + protected Spinner rotor2View; + protected Spinner rotor3View; - private Spinner rotor1PositionView; - private Spinner rotor2PositionView; - private Spinner rotor3PositionView; - private Spinner reflectorPositionView; + protected Spinner rotor1PositionView; + protected Spinner rotor2PositionView; + protected Spinner rotor3PositionView; + protected Spinner reflectorPositionView; public LayoutContainer_T() { @@ -47,38 +48,13 @@ public class LayoutContainer_T extends LayoutContainer this.resetLayout(); } - @Override - public void doCrypto() - { - if(inputView.getText().length()!=0) - { - syncStateFromLayoutToEnigma(); - String message = inputView.getText().toString(); - // - boolean egg = false; - if(message.hashCode() == -1475861192) egg = true; - message = inputPreparer.prepareString(message); - input.setText(message); - if(egg) output.setText("ENIGMA rotor1Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor1View.setAdapter(rotor1Adapter); + ArrayAdapter rotor2Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor2View.setAdapter(rotor2Adapter); + ArrayAdapter rotor3Adapter = ArrayAdapter.createFromResource(main, R.array.rotors_1_8, + android.R.layout.simple_spinner_item); + rotor3Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + rotor3View.setAdapter(rotor3Adapter); + + ArrayAdapter rotor1PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + rotor1PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor1PositionView.setAdapter(rotor1PositionAdapter); + ArrayAdapter rotor2PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + rotor2PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor2PositionView.setAdapter(rotor2PositionAdapter); + ArrayAdapter rotor3PositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + rotor3PositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + rotor3PositionView.setAdapter(rotor3PositionAdapter); + + ArrayAdapter reflectorPositionAdapter = new ArrayAdapter<>(main.getApplicationContext(), + android.R.layout.simple_spinner_item, rotorPositionArray); + reflectorPositionAdapter.setDropDownViewResource( + android.R.layout.simple_spinner_dropdown_item); + reflectorPositionView.setAdapter(reflectorPositionAdapter); } @Override @@ -108,11 +111,12 @@ public class LayoutContainer_T extends LayoutContainer } @Override - public void setLayoutState(EnigmaStateBundle state) + protected void setLayoutState(EnigmaStateBundle state) { - this.rotor1View.setSelection(state.getTypeRotor1()); - this.rotor2View.setSelection(state.getTypeRotor2()); - this.rotor3View.setSelection(state.getTypeRotor3()); + this.state = state; + this.rotor1View.setSelection(state.getTypeRotor1() - 18); + this.rotor2View.setSelection(state.getTypeRotor2() - 18); + this.rotor3View.setSelection(state.getTypeRotor3() - 18); this.rotor1PositionView.setSelection(state.getRotationRotor1()); this.rotor2PositionView.setSelection(state.getRotationRotor2()); @@ -121,23 +125,21 @@ public class LayoutContainer_T extends LayoutContainer } @Override - public void syncStateFromLayoutToEnigma() + protected void refreshState() { - EnigmaStateBundle state = getEnigma().getState(); - state.setTypeRotor1(rotor1View.getSelectedItemPosition()); - state.setTypeRotor2(rotor2View.getSelectedItemPosition()); - state.setTypeRotor3(rotor3View.getSelectedItemPosition()); + state.setTypeRotor1(rotor1View.getSelectedItemPosition() + 18); + state.setTypeRotor2(rotor2View.getSelectedItemPosition() + 18); + state.setTypeRotor3(rotor3View.getSelectedItemPosition() + 18); state.setRotationRotor1(rotor1PositionView.getSelectedItemPosition()); state.setRotationRotor2(rotor2PositionView.getSelectedItemPosition()); state.setRotationRotor3(rotor3PositionView.getSelectedItemPosition()); state.setRotationReflector(reflectorPositionView.getSelectedItemPosition()); - getEnigma().setState(state); } @Override public void showRingSettingsDialog() { new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef(). - createRingSettingsDialog(getEnigma().getState()); + createRingSettingsDialog(state); } } diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java deleted file mode 100644 index e6f5b38..0000000 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java +++ /dev/null @@ -1,109 +0,0 @@ -/** - * 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 3 of the License, or - * 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, see . - */ -package de.vanitasvitae.enigmandroid.layout; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.DialogInterface; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.Toast; - -import de.vanitasvitae.enigmandroid.MainActivity; -import de.vanitasvitae.enigmandroid.R; - -/** - * Builder for the dialog that is used to obtain a passphrase to generate - * a enigma configuration from it. - * Alternatively the user can enter the content String from a EnigmAndroid QR-Code here. - * That would have the same effect as scanning the QR-Code. - * Copyright (C) 2015 Paul Schaub - */ -public class PassphraseDialogBuilder -{ - private final MainActivity main; - private final View passphraseDialogView; - private final EditText passphrase; - private Button positive; - public PassphraseDialogBuilder() - { - main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity(); - passphraseDialogView = View.inflate(main, R.layout.dialog_passphrase, null); - passphrase = (EditText) passphraseDialogView.findViewById(R.id.passphrase); - passphrase.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - //Do nothing - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - //Do nothing - } - - @Override - public void afterTextChanged(Editable s) { - //Count input text and enable positive button if length > 0. - //Disable else - if(s.length() > 0) positive.setEnabled(true); - else positive.setEnabled(false); - } - }); - } - - /** - * create and show the dialog - */ - public void showDialog() - { - - AlertDialog.Builder builder = new AlertDialog.Builder(main); - builder.setTitle(R.string.hint_configuration); - Dialog d = builder.setView(passphraseDialogView) - .setCancelable(true) - .setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, int id) - { - String pass = passphrase.getText().toString(); - if(pass.startsWith(MainActivity.APP_ID+"/")) - { - main.restoreStateFromCode(pass); - Toast.makeText(main, R.string.dialog_passphrase_was_coded_state, Toast.LENGTH_LONG).show(); - } - else - { - main.applyStateFromSeed(pass); - String message = String.format(main.getResources().getString( - R.string.dialog_passphrase_set), " \'"+pass+"\'"); - Toast.makeText(main, message, Toast.LENGTH_LONG).show(); - } - } - }) - .setNegativeButton(R.string.dialog_negative, new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.cancel(); - Toast.makeText(main, R.string.dialog_abort, - Toast.LENGTH_SHORT).show(); - } - }).create(); - d.show(); - positive = ((AlertDialog)d).getButton(AlertDialog.BUTTON_POSITIVE); - positive.setEnabled(false); - } -} \ No newline at end of file diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java index 6a40d29..df5a561 100644 --- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java +++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java @@ -1,59 +1,49 @@ -/** - * 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 3 of the License, or - * 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, see . - */ package de.vanitasvitae.enigmandroid.layout; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; -import android.content.res.Resources; +import android.graphics.drawable.Drawable; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; import de.vanitasvitae.enigmandroid.MainActivity; import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; /** - * Builder for the dialog that is used to plug the plugboard/wire the - * rewirable reflector. + * Builder for the dialog that is used to get settings for the rings * Copyright (C) 2015 Paul Schaub + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * @author vanitasvitae */ -class PluggableDialogBuilder +public class PluggableDialogBuilder { - private ArrayList buttons; - private View dialogView; - private final MainActivity main; - private final EnigmaStateBundle state; + protected ArrayList