diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3e83728
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,29 @@
+tmp/
+*.tmp
+*.bak
+*.log
+*~*
+*.iml
+out/
+.idea/
+*.ipr
+*.iws
+.gradle
+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.txt b/CHANGELOG.txt
old mode 100755
new mode 100644
index 6da5ff1..43e1e5f
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -1,15 +1,34 @@
CHANGELOG ENIGMANDROID
+v1.0.0-not-yet-released<
+*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
+*TODO?: Move KD right below K
+*TODO?: Add intent filters to recognize and automatically handle shared/copied configuration Strings
+ TODO: These are Strings starting with "EnigmAndroid/"
+*TODO: Write tests to ensure correct functionality (Pull Requests welcome)
+*TODO: Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon)
+
+
v0.1.9-09.10.2015<
*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
+*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)
-*TODO: Write tests to ensure correct functionality
-*TODO: Migrate preferences to SettingsActivity
-*TODO: Add multi-Enigma (select any rotor/reflector etc. Probably wont happen too soon)
v0.1.8-27.09.2015<
*Added Enigma G31
diff --git a/EnigmAndroid.iml b/EnigmAndroid.iml
deleted file mode 100755
index fb2763b..0000000
--- a/EnigmAndroid.iml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/License.md b/License.md
old mode 100755
new mode 100644
diff --git a/README.md b/README.md
old mode 100755
new mode 100644
diff --git a/app/.gitignore b/app/.gitignore
new file mode 100644
index 0000000..fa69b30
--- /dev/null
+++ b/app/.gitignore
@@ -0,0 +1,4 @@
+/build
+/src/main/java/de/vanitasvitae/enigmandroid/enigma/util/
+/src/androidTest/
+/src/main/res/render-icon.sh
diff --git a/app/EnigmAndroid.apk b/app/EnigmAndroid.apk
new file mode 100644
index 0000000..d64c125
Binary files /dev/null and b/app/EnigmAndroid.apk differ
diff --git a/app/app.iml b/app/app.iml
index c183b64..d031a27 100644
--- a/app/app.iml
+++ b/app/app.iml
@@ -12,10 +12,7 @@
-
-
- generateDebugAndroidTestSourcesgenerateDebugSources
@@ -28,7 +25,7 @@
-
+
@@ -50,6 +47,15 @@
+
+
+
+
+
+
+
+
+
@@ -57,6 +63,15 @@
+
+
+
+
+
+
+
+
+
@@ -64,32 +79,30 @@
+
+
-
-
-
-
+
-
-
-
-
+
+
+
-
-
+
+
-
-
+
+
\ No newline at end of file
diff --git a/app/build.gradle b/app/build.gradle
old mode 100755
new mode 100644
index d57b91b..e186f94
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
- buildToolsVersion "22.0.1"
+ buildToolsVersion "23.0.2"
defaultConfig {
applicationId "de.vanitasvitae.enigmandroid"
@@ -19,5 +19,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
- compile 'com.android.support:support-v4:23.0.1'
+ compile 'com.android.support:support-v4:23.1.1'
}
diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro
old mode 100755
new mode 100644
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
old mode 100755
new mode 100644
index 284c739..9a0a91b
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -16,6 +16,7 @@
+
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
index 231d4d9..2a59e5c 100644
--- a/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java
+++ b/app/src/main/java/com/google/zxing/integration/android/IntentIntegrator.java
@@ -147,7 +147,7 @@ public class IntentIntegrator {
private String buttonYes;
private String buttonNo;
private List targetApplications;
- private final Map moreExtras = new HashMap(3);
+ private final Map moreExtras = new HashMap<>(3);
/**
* @param activity {@link Activity} invoking the integration
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
index 2469af9..9e452fb 100644
--- a/app/src/main/java/com/google/zxing/integration/android/IntentResult.java
+++ b/app/src/main/java/com/google/zxing/integration/android/IntentResult.java
@@ -82,14 +82,8 @@ public final class IntentResult {
@Override
public String toString() {
- StringBuilder dialogText = new StringBuilder(100);
- dialogText.append("Format: ").append(formatName).append('\n');
- dialogText.append("Contents: ").append(contents).append('\n');
int rawBytesLength = rawBytes == null ? 0 : rawBytes.length;
- dialogText.append("Raw bytes: (").append(rawBytesLength).append(" bytes)\n");
- dialogText.append("Orientation: ").append(orientation).append('\n');
- dialogText.append("EC level: ").append(errorCorrectionLevel).append('\n');
- return dialogText.toString();
+ 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 100755
new mode 100644
index 6f73265..c4d7602
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/MainActivity.java
@@ -1,20 +1,27 @@
package de.vanitasvitae.enigmandroid;
+import android.annotation.SuppressLint;
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.LayoutInflater;
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;
@@ -51,418 +58,514 @@ import de.vanitasvitae.enigmandroid.layout.PassphraseDialogBuilder;
*/
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";
+ 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 LayoutContainer layoutContainer;
- private String prefMachineType;
- private String prefNumericLanguage;
- private String prefMessageFormatting;
+ private LayoutContainer layoutContainer;
- private SecureRandom secureRandom;
+ private SecureRandom secureRandom;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- secureRandom = new SecureRandom();
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
- getStringArray(R.array.pref_alias_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 whats-new dialog
+ handleVersionUpdate();
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- this.updateContentView();
- }
+ //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);
+ }
+ }
+ }
+ }
- private void updateContentView()
- {
- switch (prefMachineType)
- {
- case "I":
- 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":
- case "KS":
- case "KSA":
- case "T":
- case "R":
- case "G31":
- case "G312":
- case "G260":
- this.setContentView(R.layout.activity_main_g_k_r_t);
- break;
- default:
- this.setContentView(R.layout.activity_main_i_m3);
- break;
- }
- }
+ public SecureRandom getSecureRandom()
+ {
+ return this.secureRandom;
+ }
- private void updatePreferenceValues()
- {
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- this.setPrefMachineType(sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
- getStringArray(R.array.pref_alias_machine_type)[0]));
- this.setPrefNumericLanguage(sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources().
- getStringArray(R.array.pref_alias_numeric_spelling_language)[0]));
- this.setPrefMessageFormatting(sharedPreferences.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources().
- getStringArray(R.array.pref_alias_message_formatting)[0]));
- }
+ public void onDialogFinished(EnigmaStateBundle state)
+ {
+ layoutContainer.getEnigma().setState(state);
+ }
- private 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());
- layoutContainer.getInput().setText(savedInput);
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- sharedPreferences.edit().putString(SettingsActivity.PREF_MACHINE_TYPE, type).apply();
- }
- }
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu)
+ {
+ this.getMenuInflater().inflate(R.menu.main, menu);
+ return true;
+ }
- public String getPrefMachineType()
- {
- if(prefMachineType != null) return prefMachineType;
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
- getStringArray(R.array.pref_alias_machine_type)[0]);
- return prefMachineType;
- }
+ @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);
+ }
- public void setPrefNumericLanguage(String lang)
- {
- if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang))
- {
- prefNumericLanguage = lang;
- layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
- }
- }
+ /**
+ * 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();
+ }
- public String getPrefNumericLanguage()
- {
- if(prefNumericLanguage != null) return prefNumericLanguage;
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- this.prefNumericLanguage = sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources().
- getStringArray(R.array.pref_alias_numeric_spelling_language)[0]);
- return prefNumericLanguage;
- }
+ /**
+ * 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);
+ }
- public void setPrefMessageFormatting(String messageFormatting)
- {
- if(prefMessageFormatting == null || !prefMessageFormatting.equals(messageFormatting))
- {
- prefMessageFormatting = messageFormatting;
- layoutContainer.setEditTextAdapter(messageFormatting);
- }
- }
+ /**
+ * 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)));
+ }
- public String getPrefMessageFormatting()
- {
- if(prefMessageFormatting != null) return prefMessageFormatting;
- SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- this.prefMessageFormatting = sharedPreferences.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources().
- getStringArray(R.array.pref_alias_message_formatting)[0]);
- return prefMessageFormatting;
- }
+ /**
+ * Start the barcode app to scan a barcode for configuration
+ */
+ private void receiveConfigurationQR()
+ {
+ IntentIntegrator integrator = new IntentIntegrator(this);
+ integrator.initiateScan();
+ }
- public SecureRandom getSecureRandom()
- {
- return this.secureRandom;
- }
+ /**
+ * Show a dialog to restore a configuration
+ */
+ private void receiveConfigurationText()
+ {
+ new PassphraseDialogBuilder().showDialog();
+ }
- public void onDialogFinished(EnigmaStateBundle state)
- {
- layoutContainer.getEnigma().setState(state);
- }
+ /**
+ * Check, whether the app has been updated
+ */
+ private void handleVersionUpdate()
+ {
+ int currentVersionNumber = 0;
+ int savedVersionNumber = SettingsActivity.SettingsSingleton.getInstance().getVersionNumber();
+ try
+ {
+ PackageInfo p = getPackageManager().getPackageInfo(getPackageName(), 0);
+ currentVersionNumber = p.versionCode;
+ }
+ catch (Exception ignored) {}
+ if(currentVersionNumber > savedVersionNumber)
+ {
+ showWhatsNewDialog();
+ SettingsActivity.SettingsSingleton.getInstance().setVersionNumber(currentVersionNumber);
+ }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- this.getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
+ }
- @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_random_configuration)
- {
- layoutContainer.getEnigma().randomState();
- layoutContainer.syncStateFromEnigmaToLayout();
- Toast.makeText(getApplicationContext(), R.string.message_random,
- Toast.LENGTH_SHORT).show();
- return true;
- }
- else if (id == R.id.action_choose_ringsetting)
- {
- 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)));
- }
- }
- else if (id == R.id.action_receive_scan)
- {
- IntentIntegrator integrator = new IntentIntegrator(this);
- integrator.initiateScan();
- return true;
- }
- else if(id == R.id.action_share_scan)
- {
- IntentIntegrator QRIntegrator = new IntentIntegrator(this);
- layoutContainer.syncStateFromLayoutToEnigma();
- Log.d(APP_ID, "Sharing configuration to QR: " + layoutContainer.getEnigma().stateToString());
- QRIntegrator.shareText(APP_ID+"/"+layoutContainer.getEnigma().stateToString());
- return true;
- }
- else if(id == R.id.action_enter_seed)
- {
- new PassphraseDialogBuilder().showDialog();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- /**
- * 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();
- }
-
- /**
- * 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);
+ /**
+ * Show a dialog that informs the user about the latest important changes in the app
+ * The dialog appears whenever the app starts after an update or after data has been
+ * deleted
+ */
+ private void showWhatsNewDialog()
+ {
+ PackageInfo pInfo = null;
+ try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);}
+ catch (PackageManager.NameNotFoundException e){ e.printStackTrace();}
+ assert pInfo != null;
+ String version = pInfo.versionName;
+ LayoutInflater li = LayoutInflater.from(this);
+ @SuppressLint("InflateParams")
+ View dialog = li.inflate(R.layout.dialog_whats_new, null);
+ ((TextView) dialog.findViewById(R.id.dialog_whats_new_header)).setText(
+ String.format(getResources().getText(R.string.dialog_whats_new_header).toString(),version));
+ ((TextView) dialog.findViewById(R.id.dialog_whats_new_details)).setText(
+ R.string.dialog_whats_new_content);
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
+ builder.setView(dialog).setTitle(R.string.dialog_whats_new_title)
+ .setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener()
+ {
+ @Override
+ public void onClick(DialogInterface dialog, int which)
+ {
+ dialog.dismiss();
+ }
+ });
+ builder.create().show();
+ }
+ /**
+ * 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();}
- String version = pInfo.versionName+ " ("+pInfo.versionCode+")";
- TextView versionText = (TextView) aboutView.findViewById(R.id.about_version_section);
- versionText.setText(version);
+ 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);
//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();
- }
+ 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();
+ }
- /**
- * 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);
+ /**
+ * 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();
+ }
- switch (requestCode) {
- case RESULT_SETTINGS:
- {
- SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
- this.setPrefMachineType(sharedPrefs.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources()
- .getStringArray(R.array.pref_alias_machine_type)[0]));
- this.setPrefNumericLanguage(sharedPrefs.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources().
- getStringArray(R.array.pref_alias_numeric_spelling_language)[0]));
- this.setPrefMessageFormatting(sharedPrefs.getString(SettingsActivity.PREF_MESSAGE_FORMATTING,
- getResources().getStringArray(R.array.pref_alias_message_formatting)[0]));
- break;
- }
- 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);
- }
- }
- }
- }
+ /**
+ * 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();
+ }
- /**
- * 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
- {
- mem = mem.substring((APP_ID+"/").length());
- BigInteger s = new BigInteger(mem, 16);
- Log.d(APP_ID, "Try to restore configuration from BigInteger value "+ s.toString());
- setPrefMachineType(Enigma.chooseEnigmaFromSave(s));
- updateContentView();
- layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType());
- layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20));
- layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
- layoutContainer.syncStateFromEnigmaToLayout();
- }
- }
+ /**
+ * 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);
+ }
+ }
+ }
+ }
- /**
- * Set EnigmAndroid into a state calculated from the seed.
- * @param seed seed
- */
- public void createStateFromSeed(String seed)
- {
- setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed));
- updateContentView();
- layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType());
- layoutContainer.getEnigma().setStateFromSeed(seed);
- layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
- layoutContainer.syncStateFromEnigmaToLayout();
- }
+ /**
+ * 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());
+ }
+ }
- /**
- * 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);
- }
- }
+ /**
+ * 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("");
+ }
+ }
-/**
- * Singleton that grants access to an Activity from anywhere within the app
- */
-public static class ActivitySingleton
-{
- private static ActivitySingleton instance = null;
- private Activity activity;
+ /**
+ * 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("");
+ }
- //private constructor
- private ActivitySingleton(){}
- //Singleton method
- public static ActivitySingleton getInstance()
- {
- if(instance == null) instance = new ActivitySingleton();
- return instance;
- }
+ /**
+ * 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);
+ }
+ }
- /**
- * Set an Activity that the Singleton returns
- * @param activity activity that's stored
- */
- public void setActivity(Activity activity)
- {
- this.activity = activity;
- }
+ /**
+ * Singleton that grants access to an Activity from anywhere within the app
+ */
+ public static class ActivitySingleton
+ {
+ private static ActivitySingleton instance = null;
+ private Activity activity;
- /**
- * Returns the stored Activity
- * @return stored Activity
- */
- public Activity getActivity()
- {
- return 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 100755
new mode 100644
index 83c61c4..4e88e5c
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/SettingsActivity.java
@@ -1,41 +1,257 @@
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
+ *
+ * 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_ANOMALY = "prefAnomaly";
- public static final String PREF_MESSAGE_FORMATTING = "prefMessageFormatting";
- public static final String PREF_REPLACE_SPECIAL_CHARACTERS = "prefReplaceSpecialCharacters";
- public static final String PREF_REPLACE_SPACES = "prefReplaceSpaces";
+ 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";
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- //noinspection deprecation
- addPreferencesFromResource(R.xml.pref_page);
- }
-}
+ 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
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 100755
new mode 100644
index 41e420f..3f5cfed
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma.java
@@ -5,9 +5,9 @@ import java.util.ArrayList;
import java.util.Random;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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
@@ -31,255 +31,262 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public abstract class Enigma
{
- protected static String machineType;
+ static String machineType;
- protected boolean doAnomaly = false; //Has the time come to handle an anomaly?
+ boolean doAnomaly = false; //Has the time come to handle an anomaly?
- protected ArrayList availableEntryWheels;
- protected ArrayList availableRotors;
- protected ArrayList availableReflectors;
+ ArrayList availableEntryWheels;
+ ArrayList availableRotors;
+ ArrayList availableReflectors;
- protected Random rand;
+ Random rand;
- public Enigma()
- {
- establishAvailableParts();
- initialize();
- }
+ Enigma()
+ {
+ establishAvailableParts();
+ initialize();
+ }
- /**
- * In this method, available EntryWheels, Rotors and Reflectors can be defined.
- */
- protected abstract void establishAvailableParts();
+ /**
+ * In this method, available EntryWheels, Rotors and Reflectors can be defined.
+ */
+ protected abstract void establishAvailableParts();
- /**
- * Add a Rotor to the ArrayList of available rotors for this machine.
- * Also set the index of the Rotor.
- * @param r Rotor
- */
- protected void addAvailableRotor(Rotor r)
- {
- if(availableRotors == null) availableRotors = new ArrayList<>();
- availableRotors.add(availableRotors.size(), r.setIndex(availableRotors.size()));
- }
+ /**
+ * 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()));
+ }
- protected void addAvailableEntryWheel(EntryWheel e)
- {
- if(availableEntryWheels == null) availableEntryWheels = new ArrayList<>();
- availableEntryWheels.add(availableEntryWheels.size(), e.setIndex(availableEntryWheels.size()));
- }
+ void addAvailableEntryWheel(EntryWheel e)
+ {
+ if(availableEntryWheels == null) availableEntryWheels = new ArrayList<>();
+ availableEntryWheels.add(availableEntryWheels.size(), e.setIndex(availableEntryWheels.size()));
+ }
- protected void addAvailableReflector(Reflector r)
- {
- if(availableReflectors == null) availableReflectors = new ArrayList<>();
- availableReflectors.add(availableReflectors.size(), r.setIndex(availableReflectors.size()));
- }
+ void addAvailableReflector(Reflector r)
+ {
+ if(availableReflectors == null) availableReflectors = new ArrayList<>();
+ availableReflectors.add(availableReflectors.size(), r.setIndex(availableReflectors.size()));
+ }
- public ArrayList getAvailableEntryWheels()
- {
- return availableEntryWheels;
- }
+ public ArrayList getAvailableEntryWheels()
+ {
+ return availableEntryWheels;
+ }
- public ArrayList getAvailableRotors()
- {
- return availableRotors;
- }
+ public ArrayList getAvailableRotors()
+ {
+ return availableRotors;
+ }
- public ArrayList getAvailableReflectors()
- {
- return availableReflectors;
- }
+ public ArrayList getAvailableReflectors()
+ {
+ return availableReflectors;
+ }
- public EntryWheel getEntryWheel(int index)
- {
- if(availableEntryWheels == null || availableEntryWheels.size() == 0) return null;
- return availableEntryWheels.get(index % availableEntryWheels.size()).getInstance();
- }
+ EntryWheel getEntryWheel(int index)
+ {
+ if(availableEntryWheels == null || availableEntryWheels.size() == 0) return null;
+ return availableEntryWheels.get(index % availableEntryWheels.size()).getInstance();
+ }
- public Rotor getRotor(int index)
- {
- if(availableRotors == null || availableRotors.size() == 0) return null;
- return availableRotors.get(index % availableRotors.size()).getInstance();
- }
+ Rotor getRotor(int index)
+ {
+ if(availableRotors == null || availableRotors.size() == 0) return null;
+ return availableRotors.get(index % availableRotors.size()).getInstance();
+ }
- public Rotor getRotor(int index, int rotation, int ringSetting)
- {
- return getRotor(index).setRotation(rotation).setRingSetting(ringSetting);
- }
+ Rotor getRotor(int index, int rotation, int ringSetting)
+ {
+ return getRotor(index).setRotation(rotation).setRingSetting(ringSetting);
+ }
- public Reflector getReflector(int index)
- {
- if(availableReflectors == null || availableReflectors.size() == 0) return null;
- return availableReflectors.get(index % availableReflectors.size()).getInstance();
- }
+ Reflector getReflector(int index)
+ {
+ if(availableReflectors == null || availableReflectors.size() == 0) return null;
+ return availableReflectors.get(index%availableReflectors.size()).getInstance();
+ }
- public Reflector getReflector(int index, int rotation, int ringSetting)
- {
- return getReflector(index).setRotation(rotation).setRingSetting(ringSetting);
- }
+ Reflector getReflector(int index, int rotation, int ringSetting)
+ {
+ return getReflector(index).setRotation(rotation).setRingSetting(ringSetting);
+ }
- /**
- * Set the enigma to an initial state
- */
- public abstract void initialize();
+ /**
+ * 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;
- }
+ /**
+ * 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 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 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();
+ /**
+ * 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
- */
- public abstract char encryptChar(char k);
+ /**
+ * 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);
+ /**
+ * 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();
+ /**
+ * 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();
- }
+ /**
+ * 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);
+ public abstract void restoreState(BigInteger mem, int protocol_version);
- public abstract String stateToString();
+ public BigInteger getEncodedState()
+ {
+ return getEncodedState(MainActivity.latest_protocol_version);
+ }
+ protected abstract BigInteger getEncodedState(int protocol_version);
- public static String numToMachineType(int n)
- {
- n = (12+(n+12)%12)%12; //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";
- default: return "T";
- }
- }
+ 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() % 12);
- }
+ public static String chooseEnigmaFromSeed(String seed)
+ {
+ return numToMachineType(seed.hashCode() % 13);
+ }
- public static String chooseEnigmaFromSave(BigInteger save)
- {
- return numToMachineType(getValue(save,20));
- }
+ 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;
- }
+ /**
+ * 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());
- }
+ /**
+ *
+ * @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;
- }
+ /**
+ * 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;
- }
+ /**
+ *
+ * @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 7eccf24..3a68944 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/EnigmaStateBundle.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/EnigmaStateBundle.java
@@ -49,15 +49,19 @@ public class EnigmaStateBundle{
private int[] configurationReflector;
- public String getMachineType()
- {
- return this.machineType;
- }
+// --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)
- public void setMachineType(String type)
- {
- this.machineType = type;
- }
+// --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;
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 c513df3..bce6a04 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
@@ -5,9 +5,10 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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
@@ -31,182 +32,191 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_D extends Enigma {
- protected EntryWheel entryWheel;
- protected Rotor rotor1;
- protected Rotor rotor2;
- protected Rotor rotor3;
- protected Reflector reflector;
+ private EntryWheel entryWheel;
+ private Rotor rotor1;
+ private Rotor rotor2;
+ private Rotor rotor3;
+ private Reflector reflector;
- public Enigma_D()
- {
- super();
- machineType = "D";
- Log.d(MainActivity.APP_ID, "Created Enigma D");
- }
+ public Enigma_D()
+ {
+ super();
+ machineType = "D";
+ Log.d(MainActivity.APP_ID, "Created Enigma D");
+ }
- @Override
- protected void establishAvailableParts()
- {
- addAvailableEntryWheel(new EntryWheel.EntryWheel_QWERTZ());
+ @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));
+ 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.ReflectorEnigma_D_KD_G31());
- }
+ addAvailableReflector(new Reflector.ReflectorEnigma_D_G31());
+ }
- @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 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
+ 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 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
+ 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);
- this.rotor1.setRotation(rot1).setRingSetting(ring1);
- this.rotor2.setRotation(rot2).setRingSetting(ring2);
- this.rotor3.setRotation(rot3).setRingSetting(ring3);
+ this.rotor1.setRotation(rot1).setRingSetting(ring1);
+ this.rotor2.setRotation(rot2).setRingSetting(ring2);
+ this.rotor3.setRotation(rot3).setRingSetting(ring3);
- this.reflector.setRotation(rotRef).setRingSetting(ringRef)
- .setConfiguration(Plugboard.seedToReflectorConfiguration(rand));
- }
+ 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 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 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();
+ @Override
+ public EnigmaStateBundle getState()
+ {
+ EnigmaStateBundle state = new EnigmaStateBundle();
- state.setTypeEntryWheel(entryWheel.getIndex());
+ state.setTypeEntryWheel(entryWheel.getIndex());
- state.setTypeRotor1(rotor1.getIndex());
- state.setTypeRotor2(rotor2.getIndex());
- state.setTypeRotor3(rotor3.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.setRotationRotor1(rotor1.getRotation());
+ state.setRotationRotor2(rotor2.getRotation());
+ state.setRotationRotor3(rotor3.getRotation());
- state.setRingSettingRotor1(rotor1.getRingSetting());
- state.setRingSettingRotor2(rotor2.getRingSetting());
- state.setRingSettingRotor3(rotor3.getRingSetting());
+ 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());
+ state.setTypeReflector(reflector.getIndex());
+ state.setRotationReflector(reflector.getRotation());
+ state.setRingSettingReflector(reflector.getRingSetting());
+ state.setConfigurationReflector(reflector.getConfiguration());
- return state;
- }
+ return state;
+ }
- @Override
- public void restoreState(BigInteger s)
- {
- 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);
+ @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);
- }
+ 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;
- @Override
- public String stateToString() {
- 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
- return s.toString(16);
- }
+ 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;
+ }
}
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
index e78ceee..46e85b4 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G260.java
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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)
@@ -30,41 +30,42 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_G260 extends Enigma_G31
{
- public Enigma_G260()
- {
- super();
- machineType = "G260";
- Log.d(MainActivity.APP_ID, "Created Enigma G260");
- }
+ 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
+ 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 String stateToString()
- {
- 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);
+ @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, 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, 5, 20); //Machine #5
+ s = addDigit(s, protocol_version, MainActivity.max_protocol_version);
- return s.toString(16);
- }
+ 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
index e751e4f..90d8cea 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G31.java
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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)
@@ -30,194 +30,202 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_G31 extends Enigma
{
- protected EntryWheel entryWheel;
- protected Rotor rotor1;
- protected Rotor rotor2;
- protected Rotor rotor3;
- protected Reflector reflector;
+ 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");
- }
+ 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_KD_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 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);
- }
- }
- }
- }
+ @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;
+ 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);
+ 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.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 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 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();
+ @Override
+ public EnigmaStateBundle getState() {
+ EnigmaStateBundle state = new EnigmaStateBundle();
- state.setTypeEntryWheel(entryWheel.getIndex());
+ state.setTypeEntryWheel(entryWheel.getIndex());
- state.setTypeRotor1(rotor1.getIndex());
- state.setTypeRotor2(rotor2.getIndex());
- state.setTypeRotor3(rotor3.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.setRotationRotor1(rotor1.getRotation());
+ state.setRotationRotor2(rotor2.getRotation());
+ state.setRotationRotor3(rotor3.getRotation());
- state.setRingSettingRotor1(rotor1.getRingSetting());
- state.setRingSettingRotor2(rotor2.getRingSetting());
- state.setRingSettingRotor3(rotor3.getRingSetting());
+ 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.setTypeReflector(reflector.getIndex());
+ state.setRotationReflector(reflector.getRotation());
+ state.setRingSettingReflector(reflector.getRingSetting());
- return state;
- }
+ return state;
+ }
- @Override
- public void restoreState(BigInteger s)
- {
- 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());
+ @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);
+ 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);
- }
+ 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;
- @Override
- public String stateToString()
- {
- 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);
+ default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version);
+ }
+ }
- s = addDigit(s, rotor3.getIndex(), availableRotors.size());
- s = addDigit(s, rotor2.getIndex(), availableRotors.size());
- s = addDigit(s, rotor1.getIndex(), availableRotors.size());
+ @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, 3, 20); //Machine #3
- return s.toString(16);
- }
+ 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
index d820b17..841afe7 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_G312.java
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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)
@@ -30,41 +30,42 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_G312 extends Enigma_G31
{
- public Enigma_G312()
- {
- super();
- machineType = "G312";
- Log.d(MainActivity.APP_ID, "Created Enigma G312");
- }
+ 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
+ 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 String stateToString()
- {
- 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);
+ @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, 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, 4, 20); //Machine #4
+ s = addDigit(s, protocol_version, MainActivity.max_protocol_version);
- return s.toString(16);
- }
+ 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 ba65ffc..6590202 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
@@ -5,9 +5,10 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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 I
@@ -30,200 +31,210 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_I extends Enigma
{
- protected EntryWheel entryWheel;
- protected Rotor rotor1;
- protected Rotor rotor2;
- protected Rotor rotor3;
- protected Reflector reflector;
+ EntryWheel entryWheel;
+ Rotor rotor1;
+ Rotor rotor2;
+ Rotor rotor3;
+ Reflector reflector;
- protected Plugboard plugboard;
+ Plugboard plugboard;
- public Enigma_I()
- {
- super();
- machineType = "I";
- Log.d(MainActivity.APP_ID, "Created Enigma I");
- }
+ public Enigma_I()
+ {
+ super();
+ machineType = "I";
+ Log.d(MainActivity.APP_ID, "Created Enigma I");
+ }
- @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());
- }
+ @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());
+ }
- @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.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)
- {
- 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
- 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
+ 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);
- 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);
+ 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.rotor1 = getRotor(r1, rot1, ring1);
- this.rotor2 = getRotor(r2, rot2, ring2);
- this.rotor3 = getRotor(r3, rot3, ring3);
- this.reflector = getReflector(ref);
+ 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));
- }
+ this.plugboard = new Plugboard();
+ plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(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 = 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
- }
+ @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
+ }
- @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());
- }
+ @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());
+ }
- @Override
- public EnigmaStateBundle getState()
- {
- EnigmaStateBundle state = new EnigmaStateBundle();
+ @Override
+ public EnigmaStateBundle getState()
+ {
+ EnigmaStateBundle state = new EnigmaStateBundle();
- state.setConfigurationPlugboard(plugboard.getConfiguration());
+ state.setConfigurationPlugboard(plugboard.getConfiguration());
- state.setTypeEntryWheel(entryWheel.getIndex());
+ state.setTypeEntryWheel(entryWheel.getIndex());
- state.setTypeRotor1(rotor1.getIndex());
- state.setTypeRotor2(rotor2.getIndex());
- state.setTypeRotor3(rotor3.getIndex());
+ state.setTypeRotor1(rotor1.getIndex());
+ state.setTypeRotor2(rotor2.getIndex());
+ state.setTypeRotor3(rotor3.getIndex());
- state.setTypeReflector(reflector.getIndex());
+ state.setTypeReflector(reflector.getIndex());
- state.setRotationRotor1(rotor1.getRotation());
- state.setRotationRotor2(rotor2.getRotation());
- state.setRotationRotor3(rotor3.getRotation());
+ 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.setRingSettingRotor1(rotor1.getRingSetting());
+ state.setRingSettingRotor2(rotor2.getRingSetting());
+ state.setRingSettingRotor3(rotor3.getRingSetting());
- return state;
- }
+ return state;
+ }
- @Override
- public void restoreState(BigInteger s)
- {
- 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);
+ @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.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);
- }
+ this.plugboard = new Plugboard();
+ plugboard.setConfiguration(s);
+ break;
- @Override
- public String stateToString() {
- 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
+ default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version);
+ }
+ }
- return s.toString(16);
- }
+ @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;
+ }
}
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 adfd0b5..d57a953 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
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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
@@ -30,189 +30,199 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_K extends Enigma
{
- protected EntryWheel entryWheel;
- protected Rotor rotor1;
- protected Rotor rotor2;
- protected Rotor rotor3;
- protected Reflector reflector;
+ private EntryWheel entryWheel;
+ Rotor rotor1;
+ Rotor rotor2;
+ Rotor rotor3;
+ Reflector reflector;
- public Enigma_K()
- {
- super();
- machineType = "K";
- Log.d(MainActivity.APP_ID, "Created Enigma K");
- }
+ public Enigma_K()
+ {
+ super();
+ machineType = "K";
+ Log.d(MainActivity.APP_ID, "Created Enigma K");
+ }
- @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());
- }
+ @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());
+ }
- @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 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)
- {
- 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
- 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
+ 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);
+ 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.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 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 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();
+ @Override
+ public EnigmaStateBundle getState()
+ {
+ EnigmaStateBundle state = new EnigmaStateBundle();
- state.setTypeEntryWheel(entryWheel.getIndex());
+ state.setTypeEntryWheel(entryWheel.getIndex());
- state.setTypeRotor1(rotor1.getIndex());
- state.setTypeRotor2(rotor2.getIndex());
- state.setTypeRotor3(rotor3.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.setRotationRotor1(rotor1.getRotation());
+ state.setRotationRotor2(rotor2.getRotation());
+ state.setRotationRotor3(rotor3.getRotation());
- state.setRingSettingRotor1(rotor1.getRingSetting());
- state.setRingSettingRotor2(rotor2.getRingSetting());
- state.setRingSettingRotor3(rotor3.getRingSetting());
+ 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.setTypeReflector(reflector.getIndex());
+ state.setRotationReflector(reflector.getRotation());
+ state.setRingSettingReflector(reflector.getRingSetting());
- return state;
- }
+ return state;
+ }
- @Override
- public void restoreState(BigInteger s)
- {
- 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());
+ @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);
+ 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);
- }
+ 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;
- @Override
- public String stateToString()
- {
- 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
+ default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version);
+ }
+ }
- return s.toString(16);
- }
+ @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;
+ }
}
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
new file mode 100644
index 0000000..c491835
--- /dev/null
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_KD.java
@@ -0,0 +1,241 @@
+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
+
+ 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_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
index a87032c..758282c 100644
--- 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
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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)
@@ -30,39 +30,42 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
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");
- }
+ 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
+ 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 String stateToString()
- {
- 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
+ @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);
- return s.toString(16);
- }
+ 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
index 5996921..b6a92c8 100644
--- 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
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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)
@@ -30,40 +30,43 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
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");
- }
+ 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
+ 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 String stateToString()
- {
- 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
+ @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);
- return s.toString(16);
- }
+ 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 3ce7d59..755dd26 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
@@ -5,9 +5,10 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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 model M3
@@ -30,69 +31,70 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_M3 extends Enigma_I
{
- public Enigma_M3()
- {
- super();
- machineType = "M3";
- Log.d(MainActivity.APP_ID, "Created Enigma M3");
- }
+ 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 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);
+ @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);
+ 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.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));
- }
+ this.plugboard = new Plugboard();
+ plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand));
+ }
- @Override
- public String stateToString() {
- 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
+ @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.toString(16);
- }
+ return s;
+ }
}
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 d3e0c92..003a6f1 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
@@ -6,9 +6,10 @@ import java.math.BigInteger;
import java.util.ArrayList;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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 the Enigma Machine name M4 of the german Kriegsmarine
@@ -31,281 +32,290 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_M4 extends Enigma
{
- private ArrayList availableThinRotors;
- private EntryWheel entryWheel;
- private Rotor rotor1;
- private Rotor rotor2;
- private Rotor rotor3;
+ private ArrayList availableThinRotors;
+ private EntryWheel entryWheel;
+ 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";
- Log.d(MainActivity.APP_ID, "Created Enigma M4");
- }
+ public Enigma_M4()
+ {
+ super();
+ machineType = "M4";
+ Log.d(MainActivity.APP_ID, "Created Enigma M4");
+ }
- protected void addAvailableThinRotor(Rotor r)
- {
- if(availableThinRotors == null) availableThinRotors = new ArrayList<>();
- availableThinRotors.add(availableThinRotors.size(), r.setIndex(availableThinRotors.size()));
- }
+ private void addAvailableThinRotor(Rotor r)
+ {
+ if(availableThinRotors == null) availableThinRotors = new ArrayList<>();
+ availableThinRotors.add(availableThinRotors.size(), r.setIndex(availableThinRotors.size()));
+ }
- public Rotor getThinRotor(int index)
- {
- if(availableThinRotors == null || availableThinRotors.size() == 0) return null;
- return availableThinRotors.get(index % availableThinRotors.size()).getInstance();
- }
+ private Rotor getThinRotor(int index)
+ {
+ if(availableThinRotors == null || availableThinRotors.size() == 0) return null;
+ return availableThinRotors.get(index % availableThinRotors.size()).getInstance();
+ }
- public Rotor getThinRotor(int index, int rotation, int ringSettings)
- {
- Rotor r = getThinRotor(index);
- if(r == null) return null;
- return r.setRotation(rotation).setRingSetting(ringSettings);
- }
+ 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
- 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
+ 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 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
- /**
- * 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();
- }
- }
- }
+ @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();
+ }
+ }
+ }
- @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);
+ @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);
- 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);
+ 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);
- 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);
+ 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);
- this.reflector = getReflector(ref, rotRef, ringRef);
+ this.reflector = getReflector(ref, rotRef, ringRef);
- this.plugboard = new Plugboard();
- this.plugboard.setConfiguration(Plugboard.seedToPlugboardConfiguration(rand));
- }
+ 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
+ /**
+ * 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 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());
+ @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.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.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.setRingSettingRotor1(rotor1.getRingSetting());
+ state.setRingSettingRotor2(rotor2.getRingSetting());
+ state.setRingSettingRotor3(rotor3.getRingSetting());
+ state.setRingSettingRotor4(rotor4.getRingSetting());
- state.setTypeReflector(reflector.getIndex());
+ state.setTypeReflector(reflector.getIndex());
- state.setConfigurationPlugboard(plugboard.getConfiguration());
+ state.setConfigurationPlugboard(plugboard.getConfiguration());
- return state;
- }
+ return state;
+ }
- @Override
- public void restoreState(BigInteger s)
- {
- 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());
+ @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);
+ 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);
- }
+ 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;
- @Override
- public String stateToString() {
- 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);
+ default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version);
+ }
+ }
- 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());
+ @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, 2, 20);
- return s.toString(16);
- }
+ 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;
+ }
}
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
index de370f5..efe00fb 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Enigma_R.java
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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)
@@ -30,191 +30,203 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_R extends Enigma
{
- protected EntryWheel entryWheel;
- protected Rotor rotor1;
- protected Rotor rotor2;
- protected Rotor rotor3;
+ private EntryWheel entryWheel;
+ private Rotor rotor1;
+ private Rotor rotor2;
+ private Rotor rotor3;
- protected Reflector reflector;
+ private Reflector reflector;
- public Enigma_R()
- {
- super();
- machineType = "R";
- Log.d(MainActivity.APP_ID, "Created Enigma R");
- }
+ 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
+ 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 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
+ 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;
+ @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);
+ 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.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 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 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();
+ @Override
+ public EnigmaStateBundle getState() {
+ EnigmaStateBundle state = new EnigmaStateBundle();
- state.setTypeEntryWheel(entryWheel.getIndex());
+ state.setTypeEntryWheel(entryWheel.getIndex());
- state.setTypeRotor1(rotor1.getIndex());
- state.setTypeRotor2(rotor2.getIndex());
- state.setTypeRotor3(rotor3.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.setRotationRotor1(rotor1.getRotation());
+ state.setRotationRotor2(rotor2.getRotation());
+ state.setRotationRotor3(rotor3.getRotation());
- state.setRingSettingRotor1(rotor1.getRingSetting());
- state.setRingSettingRotor2(rotor2.getRingSetting());
- state.setRingSettingRotor3(rotor3.getRingSetting());
+ 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.setTypeReflector(reflector.getIndex());
+ state.setRotationReflector(reflector.getRotation());
+ state.setRingSettingReflector(reflector.getRingSetting());
- return state;
- }
+ return state;
+ }
- @Override
- public void restoreState(BigInteger s)
- {
- 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());
+ @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);
+ 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);
- }
+ 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;
- @Override
- public String stateToString()
- {
- BigInteger t = BigInteger.valueOf(reflector.getRingSetting());
- t = addDigit(t, reflector.getRotation(), 26);
- t = addDigit(t, rotor3.getRingSetting(),26);
- t = addDigit(t, rotor3.getRotation(), 26);
- t = addDigit(t, rotor2.getRingSetting(),26);
- t = addDigit(t, rotor2.getRotation(), 26);
- t = addDigit(t, rotor1.getRingSetting(), 26);
- t = addDigit(t, rotor1.getRotation(), 26);
- t = addDigit(t, rotor3.getIndex(), availableRotors.size());
- t = addDigit(t, rotor2.getIndex(), availableRotors.size());
- t = addDigit(t, rotor1.getIndex(), availableRotors.size());
- t = addDigit(t, 10, 20); //Machine #10
+ default: Log.e(MainActivity.APP_ID, "Unsupported protocol version "+protocol_version);
+ }
- return t.toString(16);
- }
+ }
+
+ @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 a73b5f7..b9e3f04 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
@@ -5,9 +5,9 @@ import android.util.Log;
import java.math.BigInteger;
import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.rotors.EntryWheel;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Reflector;
-import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
+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 T Tirpitz
@@ -30,11 +30,11 @@ import de.vanitasvitae.enigmandroid.enigma.rotors.Rotor;
*/
public class Enigma_T extends Enigma
{
- protected EntryWheel entryWheel;
- protected Rotor rotor1;
- protected Rotor rotor2;
- protected Rotor rotor3;
- protected Reflector reflector;
+ private EntryWheel entryWheel;
+ private Rotor rotor1;
+ private Rotor rotor2;
+ private Rotor rotor3;
+ private Reflector reflector;
public Enigma_T()
{
@@ -166,53 +166,64 @@ public class Enigma_T extends Enigma
}
@Override
- public void restoreState(BigInteger s)
+ public void restoreState(BigInteger s, int protocol_version)
{
- 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());
+ 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);
+ 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);
+ }
- 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 String stateToString()
+ public BigInteger getEncodedState(int protocol_version)
{
- BigInteger t = BigInteger.valueOf(reflector.getRingSetting());
- t = addDigit(t, reflector.getRotation(), 26);
- t = addDigit(t, rotor3.getRingSetting(),26);
- t = addDigit(t, rotor3.getRotation(), 26);
- t = addDigit(t, rotor2.getRingSetting(),26);
- t = addDigit(t, rotor2.getRotation(), 26);
- t = addDigit(t, rotor1.getRingSetting(), 26);
- t = addDigit(t, rotor1.getRotation(), 26);
- t = addDigit(t, rotor3.getIndex(), availableRotors.size());
- t = addDigit(t, rotor2.getIndex(), availableRotors.size());
- t = addDigit(t, rotor1.getIndex(), availableRotors.size());
- t = addDigit(t, 11, 20); //Machine #11
+ 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);
- return t.toString(16);
+ 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/inputPreparer/EditTextAdapter.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/EditTextAdapter.java
index 89bf6d1..c530aea 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
@@ -8,10 +8,10 @@ import android.widget.EditText;
*/
public abstract class EditTextAdapter
{
- protected EditText editText;
- protected String content;
+ EditText editText;
+ String content;
- public EditTextAdapter(EditText editText)
+ EditTextAdapter(EditText editText)
{
this.editText = editText;
}
@@ -79,7 +79,7 @@ public abstract class EditTextAdapter
public static class EditTextAdapterGap extends EditTextAdapter
{
- protected int blockSize;
+ int blockSize;
public EditTextAdapterGap(EditText editText, int blockSize)
{
super(editText);
diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/InputPreparer.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/InputPreparer.java
index e0d4c52..9b88d3e 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/InputPreparer.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/inputPreparer/InputPreparer.java
@@ -27,7 +27,7 @@ import de.vanitasvitae.enigmandroid.SettingsActivity;
* @author vanitasvitae
*/
public abstract class InputPreparer {
- protected InputPreparer child;
+ InputPreparer child;
public String prepareString(String in) {
if (child != null)
@@ -67,9 +67,11 @@ public abstract class InputPreparer {
}
public static class ReplaceSpecialCharacters extends InputPreparer {
- public ReplaceSpecialCharacters() {
- this.child = null;
- }
+// --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;
@@ -85,9 +87,11 @@ public abstract class InputPreparer {
* Concrete implementation of a german InputPreparer
*/
public static class ReplaceNumbersGerman extends InputPreparer {
- public ReplaceNumbersGerman() {
- this.child = null;
- }
+// --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;
@@ -112,9 +116,11 @@ public abstract class InputPreparer {
* Concrete implementation of a french InputPreparer
*/
public static class ReplaceNumbersFrench extends InputPreparer {
- public ReplaceNumbersFrench() {
- this.child = null;
- }
+// --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;
@@ -139,9 +145,11 @@ public abstract class InputPreparer {
* Concrete implementation of an english InputPreparer
*/
public static class ReplaceNumbersEnglish extends InputPreparer {
- public ReplaceNumbersEnglish() {
- this.child = null;
- }
+// --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;
@@ -166,9 +174,11 @@ public abstract class InputPreparer {
* Concrete implementation of a spanish InputPreparer
*/
public static class ReplaceNumbersSpanish extends InputPreparer {
- public ReplaceNumbersSpanish() {
- this.child = null;
- }
+// --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;
@@ -193,9 +203,11 @@ public abstract class InputPreparer {
* Concrete implementation of a spanish InputPreparer
*/
public static class ReplaceNumbersItalian extends InputPreparer {
- public ReplaceNumbersItalian() {
- this.child = null;
- }
+// --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;
diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/EntryWheel.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/EntryWheel.java
similarity index 89%
rename from app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/EntryWheel.java
rename to app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/EntryWheel.java
index 027929b..0874c9b 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/EntryWheel.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/EntryWheel.java
@@ -1,4 +1,4 @@
-package de.vanitasvitae.enigmandroid.enigma.rotors;
+package de.vanitasvitae.enigmandroid.enigma.parts;
/**
* Implementation of several EntryWheels
@@ -20,14 +20,14 @@ package de.vanitasvitae.enigmandroid.enigma.rotors;
*/
public class EntryWheel
{
- protected int type;
- protected String name;
- protected int index;
- protected String summary;
- protected Integer[] connections;
- protected Integer[] reversedConnections;
+ private final int type;
+ private final String name;
+ private int index;
+ private final String summary;
+ private final Integer[] connections;
+ private final Integer[] reversedConnections;
- public EntryWheel(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections)
+ EntryWheel(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections)
{
this.type = type;
this.name = name;
@@ -56,7 +56,7 @@ public class EntryWheel
return this.connections[normalize(input)];
}
- public int normalize(int input)
+ private int normalize(int input)
{
return (input+this.connections.length)%this.connections.length;
}
@@ -83,7 +83,7 @@ public class EntryWheel
return this.reversedConnections[normalize(input)];
}
- public EntryWheel createEntryWheel(int type)
+ private EntryWheel createEntryWheel(int type)
{
switch(type)
{
diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Plugboard.java
similarity index 96%
rename from app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java
rename to app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Plugboard.java
index de4e454..3264b6a 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/Plugboard.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Plugboard.java
@@ -1,4 +1,4 @@
-package de.vanitasvitae.enigmandroid.enigma;
+package de.vanitasvitae.enigmandroid.enigma.parts;
import android.util.Log;
@@ -7,6 +7,7 @@ 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;
/**
@@ -48,6 +49,7 @@ public class Plugboard
this.plugs = conf;
}
+ @SuppressWarnings("UnusedReturnValue")
public BigInteger setConfiguration(BigInteger b)
{
String s = "";
@@ -87,7 +89,7 @@ public class Plugboard
public static int[] stringToConfiguration(String in)
{
String pairs = trimString(new InputPreparer.RemoveIllegalCharacters().prepareString(in));
- int[] out = empty;
+ 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))
@@ -174,7 +176,7 @@ public class Plugboard
* @param c array
* @return String representation
*/
- public static String configurationToString(int[] c)
+ private static String configurationToString(int[] c)
{
String out = "";
for(int i=0; i 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/rotors/Rotor.java b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Rotor.java
similarity index 88%
rename from app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java
rename to app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Rotor.java
index 5bb4730..99de65c 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Rotor.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/parts/Rotor.java
@@ -1,4 +1,4 @@
-package de.vanitasvitae.enigmandroid.enigma.rotors;
+package de.vanitasvitae.enigmandroid.enigma.parts;
import android.util.Log;
@@ -31,31 +31,31 @@ import de.vanitasvitae.enigmandroid.MainActivity;
public abstract class Rotor
{
/** Number of the rotor (used internally to create the Rotor via createRotor() ) */
- protected int type;
+ private final int type;
/** Identifier of the Rotor */
- protected String name;
+ private final String name;
/** Index of the Rotor in the parent machine's selection Spinner */
- protected int index;
+ private int index;
/** Summary of the connections (internal wiring) */
- protected String summary;
+ private final String summary;
/** Wiring of the rotor when the signal passes the first time */
- protected Integer[] connections;
+ private final Integer[] connections;
/** Wiring of the rotor when the signal passes the second time (inverse of the first time) */
- protected Integer[] reversedConnections;
+ private final Integer[] reversedConnections;
/** When the Rotor is at this Position and jumps one over, it also turns the next */
- protected Integer[] turnOverNotches;
+ private final Integer[] turnOverNotches;
/** Offset of the labeled ring of the rotor */
- protected int ringSetting;
+ private int ringSetting;
/** Rotation of the rotor */
- protected int rotation;
+ private int rotation;
/**
* This constructor is not accessible from outside this class file.
@@ -72,8 +72,8 @@ public abstract class Rotor
* @param ringSetting setting of the ring that holds the letters
* @param rotation rotation of the rotor
*/
- protected Rotor(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections,
- Integer[] turnOverNotches, int ringSetting, int rotation)
+ Rotor(int type, String name, String summary, Integer[] connections, Integer[] reversedConnections,
+ Integer[] turnOverNotches, int ringSetting, int rotation)
{
this.type = type;
this.name = name;
@@ -140,26 +140,30 @@ public abstract class Rotor
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_KSwiss_Standard_I(rotation, ringSetting);
- case 61: return new Rotor_KSwiss_Standard_II(rotation, ringSetting);
- case 62: return new Rotor_KSwiss_Standard_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_K_Swiss_Airforce_I(rotation, ringSetting);
- case 71: return new Rotor_K_Swiss_Airforce_II(rotation, ringSetting);
- case 72: return new Rotor_K_Swiss_Airforce_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_R_I(rotation, ringSetting);
- case 81: return new Rotor_R_II(rotation, ringSetting);
- case 82: return new Rotor_R_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_T_I(rotation, ringSetting);
- case 91: return new Rotor_T_II(rotation, ringSetting);
- case 92: return new Rotor_T_III(rotation, ringSetting);
- case 93: return new Rotor_T_IV(rotation, ringSetting);
- case 94: return new Rotor_T_V(rotation, ringSetting);
- case 95: return new Rotor_T_VI(rotation, ringSetting);
- case 96: return new Rotor_T_VII(rotation, ringSetting);
- case 97: return new Rotor_T_VIII(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;
@@ -285,7 +289,7 @@ public abstract class Rotor
* Returns the positions of the turnover notches in a array
* @return turnOverNotches
*/
- public Integer[] getTurnOverNotches()
+ private Integer[] getTurnOverNotches()
{
return this.turnOverNotches;
}
@@ -305,7 +309,7 @@ public abstract class Rotor
* of the rotor
* @return size
*/
- public int getRotorSize()
+ private int getRotorSize()
{
return this.connections.length;
}
@@ -707,6 +711,54 @@ public abstract class Rotor
}
}
+ /**
+ * 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
@@ -716,7 +768,7 @@ public abstract class Rotor
{
public Rotor_KSwiss_Standard_I(int rotation, int ringSetting)
{
- super(60, "KS-I", "PEZUOHXSCVFMTBGLRINQJWAYDK",
+ 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);
@@ -732,7 +784,7 @@ public abstract class Rotor
{
public Rotor_KSwiss_Standard_II(int rotation, int ringSetting)
{
- super(61, "KS-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT",
+ 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);
@@ -748,7 +800,7 @@ public abstract class Rotor
{
public Rotor_KSwiss_Standard_III(int rotation, int ringSetting)
{
- super(62, "KS-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC",
+ 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);
@@ -764,7 +816,7 @@ public abstract class Rotor
{
public Rotor_K_Swiss_Airforce_I(int rotation, int ringSetting)
{
- super(70, "KSA-I", "PEZUOHXSCVFMTBGLRINQJWAYDK",
+ 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);
@@ -779,7 +831,7 @@ public abstract class Rotor
{
public Rotor_K_Swiss_Airforce_II(int rotation, int ringSetting)
{
- super(71, "KSA-II", "ZOUESYDKFWPCIQXHMVBLGNJRAT",
+ 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);
@@ -794,7 +846,7 @@ public abstract class Rotor
{
public Rotor_K_Swiss_Airforce_III(int rotation, int ringSetting)
{
- super(72, "KSA-III", "EHRVXGAOBQUSIMZFLYNWKTPDJC",
+ 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);
@@ -810,7 +862,7 @@ public abstract class Rotor
{
public Rotor_R_I(int rotation, int ringSetting)
{
- super(80, "R-I", "JGDQOXUSCAMIFRVTPNEWKBLZYH",
+ 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);
@@ -826,7 +878,7 @@ public abstract class Rotor
{
public Rotor_R_II(int rotation, int ringSetting)
{
- super(81, "R-II", "NTZPSFBOKMWRCJDIVLAEYUXHGQ",
+ 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);
@@ -842,7 +894,7 @@ public abstract class Rotor
{
public Rotor_R_III(int rotation, int ringSetting)
{
- super(82, "R-III", "JVIUBHTCDYAKEQZPOSGXNRMWFL",
+ 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);
@@ -858,7 +910,7 @@ public abstract class Rotor
{
public Rotor_T_I(int rotation, int ringSetting)
{
- super(90, "T-I", "KPTYUELOCVGRFQDANJMBSWHZXI",
+ 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);
@@ -874,7 +926,7 @@ public abstract class Rotor
{
public Rotor_T_II(int rotation, int ringSetting)
{
- super(91, "T-II", "UPHZLWEQMTDJXCAKSOIGVBYFNR",
+ 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);
@@ -889,7 +941,7 @@ public abstract class Rotor
public static class Rotor_T_III extends Rotor
{
public Rotor_T_III(int rotation, int ringSetting) {
- super(92, "T-III", "QUDLYRFEKONVZAXWHMGPJBSICT",
+ 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);
@@ -905,7 +957,7 @@ public abstract class Rotor
{
public Rotor_T_IV(int rotation, int ringSetting)
{
- super(93, "T-IV", "CIWTBKXNRESPFLYDAGVHQUOJZM",
+ 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);
@@ -921,7 +973,7 @@ public abstract class Rotor
{
public Rotor_T_V(int rotation, int ringSetting)
{
- super(94, "T-V", "UAXGISNJBVERDYLFZWTPCKOHMQ",
+ 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);
@@ -937,7 +989,7 @@ public abstract class Rotor
{
public Rotor_T_VI(int rotation, int ringSetting)
{
- super(95, "T-VI", "XFUZGALVHCNYSEWQTDMRBKPIOJ",
+ 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);
@@ -953,7 +1005,7 @@ public abstract class Rotor
{
public Rotor_T_VII(int rotation, int ringSetting)
{
- super(96, "T-VII", "BJVFTXPLNAYOZIKWGDQERUCHSM",
+ 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);
@@ -969,7 +1021,7 @@ public abstract class Rotor
{
public Rotor_T_VIII(int rotation, int ringSetting)
{
- super(97, "T-VIII", "YMTPNZHWKODAJXELUQVGCBISFR",
+ 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);
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
deleted file mode 100644
index 19dd470..0000000
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/enigma/rotors/Reflector.java
+++ /dev/null
@@ -1,340 +0,0 @@
-package de.vanitasvitae.enigmandroid.enigma.rotors;
-
-import android.util.Log;
-
-import java.math.BigInteger;
-
-import de.vanitasvitae.enigmandroid.MainActivity;
-import de.vanitasvitae.enigmandroid.enigma.Enigma;
-import de.vanitasvitae.enigmandroid.enigma.Plugboard;
-
-/**
- * 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 int type;
- protected String name;
- protected int index;
- protected String summary;
- 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 name indicator of the reflector
- * @param connections wiring of the reflector as Integer array
- */
- protected 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;
- }
-
- 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
- */
- public 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_KD_G31();
- 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 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 ReflectorEnigma_D_KD_G31 extends Reflector
- {
- public static final int[] defaultWiring_D_KD_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_KD_G31()
- {
- super(20, "Ref-D", "Default: IMETCGFRAYSQBZXWLHKDVUPOJN", defaultWiring_D_KD_G31);
- }
- }
-
- /**
- * 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/layout/LayoutContainer.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java
index 6bb75de..37a8083 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer.java
@@ -6,6 +6,7 @@ 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;
@@ -33,133 +34,156 @@ import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer;
*/
public abstract class LayoutContainer
{
- protected EditText inputView;
- protected EditText outputView;
+ final EditText inputView;
+ private final EditText outputView;
- protected EditTextAdapter input;
- protected EditTextAdapter output;
+ EditTextAdapter input;
+ EditTextAdapter output;
- protected InputPreparer inputPreparer;
- protected MainActivity main;
+ InputPreparer inputPreparer;
+ final MainActivity main;
- public abstract Enigma getEnigma();
- protected abstract void initializeLayout();
- public abstract void resetLayout();
- public abstract void setLayoutState(EnigmaStateBundle state);
- public abstract void syncStateFromLayoutToEnigma();
- public void syncStateFromEnigmaToLayout()
- {
- this.setLayoutState(getEnigma().getState());
- }
- public abstract void showRingSettingsDialog();
+ 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();
- 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();
- }
+ 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 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 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 EditTextAdapter getInput()
- {
- return this.input;
- }
+ public EditTextAdapter getInput()
+ {
+ return this.input;
+ }
- public EditTextAdapter getOutput()
- {
- return this.output;
- }
+ public EditTextAdapter getOutput()
+ {
+ return this.output;
+ }
- 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 "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();
- default:
- return new LayoutContainer_I();
- }
- }
+ public static LayoutContainer createLayoutContainer()
+ {
+ return createLayoutContainer(SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType());
+ }
- /**
- * Add ArrayAdapter, contents and layouts to Spinner
- * @param view Spinner
- * @param resourceID ID of the referenced array (eg. R.array.rotor_1_8)
- */
- protected void prepareSpinnerAdapter(Spinner view, int resourceID) {
- MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity();
+ 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();
+ }
+ }
- ArrayAdapter adapter = ArrayAdapter.createFromResource(main, resourceID,
- android.R.layout.simple_spinner_item);
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- view.setAdapter(adapter);
- }
+ /**
+ * 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();
- /**
- * Add ArrayAdapter, contents and layouts to Spinner
- * @param view Spinner
- * @param array Character array
- */
- protected void prepareSpinnerAdapter(Spinner view, Character[] array)
- {
- MainActivity main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity();
- ArrayAdapter adapter = new ArrayAdapter<>(main.getApplicationContext(),
- android.R.layout.simple_spinner_item, array);
- adapter.setDropDownViewResource(
- android.R.layout.simple_spinner_dropdown_item);
- view.setAdapter(adapter);
- }
+ ArrayAdapter adapter = ArrayAdapter.createFromResource(main, resourceID,
+ android.R.layout.simple_spinner_item);
+ adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ view.setAdapter(adapter);
+ }
- 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.getApplicationContext(),
+ android.R.layout.simple_spinner_item, array);
+ adapter.setDropDownViewResource(
+ android.R.layout.simple_spinner_dropdown_item);
+ 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 8f51cda..45a4477 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
@@ -4,6 +4,7 @@ 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.EnigmaStateBundle;
import de.vanitasvitae.enigmandroid.enigma.Enigma_D;
@@ -32,10 +33,10 @@ public class LayoutContainer_D extends LayoutContainer
{
private Enigma_D enigma;
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
+ private Spinner rotor1PositionView;
+ private Spinner rotor2PositionView;
+ private Spinner rotor3PositionView;
+ private Spinner reflectorPositionView;
public LayoutContainer_D()
{
@@ -45,7 +46,13 @@ public class LayoutContainer_D extends LayoutContainer
}
@Override
- protected void initializeLayout()
+ protected void setEnigmaLayout()
+ {
+ MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_d);
+ }
+
+ @Override
+ protected void assembleLayout()
{
this.rotor1PositionView = (Spinner) main.findViewById(R.id.rotor1position);
this.rotor2PositionView = (Spinner) main.findViewById(R.id.rotor2position);
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
index 0497e3f..24c968c 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G260.java
@@ -24,18 +24,18 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_G260;
*/
public class LayoutContainer_G260 extends LayoutContainer_G31
{
- public LayoutContainer_G260()
- {
- super();
- main.setTitle("G260 - EnigmAndroid");
- this.resetLayout();
- }
+ 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("");
- }
+ @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
index 1e82510..53b7268 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_G31.java
@@ -2,6 +2,7 @@ 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;
@@ -29,89 +30,95 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_G31;
*/
public class LayoutContainer_G31 extends LayoutContainer
{
- protected Enigma enigma;
+ Enigma enigma;
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
+ private Spinner rotor1View;
+ private Spinner rotor2View;
+ private Spinner rotor3View;
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
+ private Spinner rotor1PositionView;
+ private Spinner rotor2PositionView;
+ private Spinner rotor3PositionView;
+ private Spinner reflectorPositionView;
- public LayoutContainer_G31()
- {
- super();
- main.setTitle("G31 - EnigmAndroid");
- this.resetLayout();
- }
+ public LayoutContainer_G31()
+ {
+ super();
+ main.setTitle("G31 - EnigmAndroid");
+ this.resetLayout();
+ }
- @Override
- public Enigma getEnigma() {
- return this.enigma;
- }
+ @Override
+ protected void setEnigmaLayout()
+ {
+ MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_g_k_r_t);
+ }
- @Override
- 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);
- 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);
+ @Override
+ public Enigma getEnigma() {
+ return this.enigma;
+ }
- Character[] rotorPositionArray = new Character[26];
- for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/}
+ @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);
- 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);
- }
+ Character[] rotorPositionArray = new Character[26];
+ for(int i=0; i<26; i++) {rotorPositionArray[i] = (char) (65+i); /*Fill with A..Z*/}
- @Override
- public void resetLayout() {
- enigma = new Enigma_G31();
- setLayoutState(enigma.getState());
- output.setText("");
- input.setText("");
- }
+ 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 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 resetLayout() {
+ enigma = new Enigma_G31();
+ setLayoutState(enigma.getState());
+ output.setText("");
+ input.setText("");
+ }
- @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 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 showRingSettingsDialog()
- {
- new RingSettingsDialogBuilder.RingSettingsDialogBuilderRotRotRotRef().
- createRingSettingsDialog(getEnigma().getState());
- }
+ @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_I.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_I.java
index 2c6bade..c7778d5 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
@@ -4,6 +4,7 @@ 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.EnigmaStateBundle;
import de.vanitasvitae.enigmandroid.enigma.Enigma_I;
@@ -32,13 +33,13 @@ public class LayoutContainer_I extends LayoutContainer
{
private Enigma_I enigma;
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
- protected Spinner reflectorView;
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
+ Spinner rotor1View;
+ Spinner rotor2View;
+ Spinner rotor3View;
+ Spinner reflectorView;
+ Spinner rotor1PositionView;
+ Spinner rotor2PositionView;
+ Spinner rotor3PositionView;
public LayoutContainer_I()
{
@@ -48,7 +49,13 @@ public class LayoutContainer_I extends LayoutContainer
}
@Override
- protected void initializeLayout()
+ protected void setEnigmaLayout()
+ {
+ MainActivity.ActivitySingleton.getInstance().getActivity().setContentView(R.layout.activity_main_i_m3);
+ }
+
+ @Override
+ protected void assembleLayout()
{
this.rotor1View = (Spinner) main.findViewById(R.id.rotor1);
this.rotor2View = (Spinner) main.findViewById(R.id.rotor2);
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 2b52ce5..5ee401d 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
@@ -2,6 +2,7 @@ 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;
@@ -29,16 +30,16 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_K;
*/
public class LayoutContainer_K extends LayoutContainer
{
- private Enigma_K enigma;
+ Enigma enigma;
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
+ private Spinner rotor1View;
+ private Spinner rotor2View;
+ private Spinner rotor3View;
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
+ private Spinner rotor1PositionView;
+ private Spinner rotor2PositionView;
+ private Spinner rotor3PositionView;
+ private Spinner reflectorPositionView;
public LayoutContainer_K()
{
@@ -47,13 +48,19 @@ 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 initializeLayout() {
+ 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);
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
new file mode 100644
index 0000000..022bca7
--- /dev/null
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_KD.java
@@ -0,0 +1,134 @@
+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
+
+ 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_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
index 6a0274d..f4d16a3 100644
--- 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
@@ -1,10 +1,5 @@
package de.vanitasvitae.enigmandroid.layout;
-import android.widget.Spinner;
-
-import de.vanitasvitae.enigmandroid.R;
-import de.vanitasvitae.enigmandroid.enigma.Enigma;
-import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle;
import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Standard;
/**
@@ -27,19 +22,8 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Standard;
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author vanitasvitae
*/
-public class LayoutContainer_K_Swiss extends LayoutContainer
+public class LayoutContainer_K_Swiss extends LayoutContainer_K
{
- private Enigma_K_Swiss_Standard enigma;
-
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
-
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
-
public LayoutContainer_K_Swiss()
{
super();
@@ -47,34 +31,6 @@ public class LayoutContainer_K_Swiss extends LayoutContainer
this.resetLayout();
}
- @Override
- public Enigma getEnigma() {
- return this.enigma;
- }
-
- @Override
- 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);
- 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_K_Swiss_Standard();
@@ -82,37 +38,4 @@ public class LayoutContainer_K_Swiss extends LayoutContainer
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_Airforce.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_K_Swiss_Airforce.java
index fc4826e..62a77d4 100644
--- 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
@@ -1,10 +1,5 @@
package de.vanitasvitae.enigmandroid.layout;
-import android.widget.Spinner;
-
-import de.vanitasvitae.enigmandroid.R;
-import de.vanitasvitae.enigmandroid.enigma.Enigma;
-import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle;
import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Airforce;
/**
@@ -27,19 +22,8 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_K_Swiss_Airforce;
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author vanitasvitae
*/
-public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer
+public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer_K
{
- private Enigma_K_Swiss_Airforce enigma;
-
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
-
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
-
public LayoutContainer_K_Swiss_Airforce()
{
super();
@@ -47,34 +31,6 @@ public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer
this.resetLayout();
}
- @Override
- public Enigma getEnigma() {
- return this.enigma;
- }
-
- @Override
- 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);
- 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_K_Swiss_Airforce();
@@ -82,37 +38,4 @@ public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer
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_M3.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_M3.java
index 8c9be62..25502b5 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
@@ -40,7 +40,7 @@ public class LayoutContainer_M3 extends LayoutContainer_I
}
@Override
- protected void initializeLayout()
+ protected void assembleLayout()
{
this.rotor1View = (Spinner) main.findViewById(R.id.rotor1);
this.rotor2View = (Spinner) main.findViewById(R.id.rotor2);
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 196f9e9..ea70ae2 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
@@ -4,6 +4,7 @@ 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.EnigmaStateBundle;
import de.vanitasvitae.enigmandroid.enigma.Enigma_M4;
@@ -49,6 +50,11 @@ 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()
@@ -57,7 +63,7 @@ public class LayoutContainer_M4 extends LayoutContainer
}
@Override
- protected void initializeLayout() {
+ 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);
@@ -68,7 +74,7 @@ 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);
- setPlugboardButton.setOnClickListener(new View.OnClickListener() {
+ if(setPlugboardButton != null) setPlugboardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PluggableDialogBuilder(getEnigma().getState()).showDialogPlugboard();
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
index eaaaf0f..adba852 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/LayoutContainer_R.java
@@ -2,6 +2,7 @@ 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;
@@ -31,14 +32,14 @@ public class LayoutContainer_R extends LayoutContainer
{
private Enigma_R enigma;
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
+ private Spinner rotor1View;
+ private Spinner rotor2View;
+ private Spinner rotor3View;
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
+ private Spinner rotor1PositionView;
+ private Spinner rotor2PositionView;
+ private Spinner rotor3PositionView;
+ private Spinner reflectorPositionView;
public LayoutContainer_R()
{
@@ -47,13 +48,19 @@ public class LayoutContainer_R 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 initializeLayout() {
+ 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);
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 88c2cdd..8a0f1c1 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
@@ -2,6 +2,7 @@ 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;
@@ -31,14 +32,14 @@ public class LayoutContainer_T extends LayoutContainer
{
private Enigma_T enigma;
- protected Spinner rotor1View;
- protected Spinner rotor2View;
- protected Spinner rotor3View;
+ private Spinner rotor1View;
+ private Spinner rotor2View;
+ private Spinner rotor3View;
- protected Spinner rotor1PositionView;
- protected Spinner rotor2PositionView;
- protected Spinner rotor3PositionView;
- protected Spinner reflectorPositionView;
+ private Spinner rotor1PositionView;
+ private Spinner rotor2PositionView;
+ private Spinner rotor3PositionView;
+ private Spinner reflectorPositionView;
public LayoutContainer_T()
{
@@ -65,13 +66,20 @@ public class LayoutContainer_T extends LayoutContainer
}
}
+ @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 initializeLayout() {
+ 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);
diff --git a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java
index 20ba903..59c9bef 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PassphraseDialogBuilder.java
@@ -37,9 +37,9 @@ import de.vanitasvitae.enigmandroid.R;
*/
public class PassphraseDialogBuilder
{
- private MainActivity main;
- private View passphraseDialogView;
- private EditText passphrase;
+ private final MainActivity main;
+ private final View passphraseDialogView;
+ private final EditText passphrase;
private Button positive;
public PassphraseDialogBuilder()
{
@@ -74,7 +74,7 @@ public class PassphraseDialogBuilder
{
AlertDialog.Builder builder = new AlertDialog.Builder(main);
- builder.setTitle(R.string.hint_passphrase);
+ builder.setTitle(R.string.hint_configuration);
Dialog d = builder.setView(passphraseDialogView)
.setCancelable(true)
.setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener()
@@ -89,7 +89,7 @@ public class PassphraseDialogBuilder
}
else
{
- main.createStateFromSeed(pass);
+ main.applyStateFromSeed(pass);
String message = String.format(main.getResources().getString(
R.string.dialog_passphrase_set), " \'"+pass+"\'");
Toast.makeText(main, message, Toast.LENGTH_LONG).show();
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 d32f504..7685e90 100644
--- a/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java
+++ b/app/src/main/java/de/vanitasvitae/enigmandroid/layout/PluggableDialogBuilder.java
@@ -37,19 +37,19 @@ import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle;
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author vanitasvitae
*/
-public class PluggableDialogBuilder
+class PluggableDialogBuilder
{
- protected ArrayList buttons;
- protected View dialogView;
- protected MainActivity main;
- protected EnigmaStateBundle state;
+ private ArrayList buttons;
+ private View dialogView;
+ private final MainActivity main;
+ private final EnigmaStateBundle state;
- protected boolean allowIncompleteConnections;
- protected Button positive;
+ private boolean allowIncompleteConnections;
+ private Button positive;
- protected HashSet colors;
+ private HashSet colors;
- protected int previouslyPressedButton = -1;
+ private int previouslyPressedButton = -1;
/**
* Constructor that prepares layout and buttons.
@@ -148,7 +148,7 @@ public class PluggableDialogBuilder
/**
* Initialize array of buttons, initialize background-color hashset.
*/
- public void initializeLayout()
+ private void initializeLayout()
{
buttons = new ArrayList<>();
dialogView = View.inflate(main, R.layout.dialog_plugs, null);
@@ -199,7 +199,7 @@ public class PluggableDialogBuilder
/**
* Set listeners for all buttons
*/
- public void setButtonListeners()
+ private void setButtonListeners()
{
for(int i=0; i<26; i++)
{
@@ -219,7 +219,7 @@ public class PluggableDialogBuilder
* return false otherwise
* @return boolean
*/
- protected boolean allConnectionsDone()
+ private boolean allConnectionsDone()
{
for(int i=0; i(
MainActivity.ActivitySingleton.getInstance().getActivity(),
@@ -50,7 +50,7 @@ public abstract class RingSettingsDialogBuilder
* Creates a ArrayAdapter working over an array of numbers 1 to 26.
* @return ArrayAdapter
*/
- public static ArrayAdapter createAdapter1_26()
+ private static ArrayAdapter createAdapter1_26()
{
Integer[] ringArray = new Integer[26];
for(int i=1; i<=26; i++) {ringArray[i-1] = i;}
@@ -365,7 +365,7 @@ public abstract class RingSettingsDialogBuilder
*/
public static abstract class Actions
{
- protected EnigmaStateBundle stateBundle;
+ final EnigmaStateBundle stateBundle;
public Actions(EnigmaStateBundle bundle)
{
this.stateBundle = bundle;
diff --git a/app/src/main/res/drawable-hdpi/ic_launcher.png b/app/src/main/res/drawable-hdpi/ic_launcher.png
old mode 100755
new mode 100644
index 9e6a7e8..6920ac5
Binary files a/app/src/main/res/drawable-hdpi/ic_launcher.png and b/app/src/main/res/drawable-hdpi/ic_launcher.png differ
diff --git a/app/src/main/res/drawable-mdpi/ic_launcher.png b/app/src/main/res/drawable-mdpi/ic_launcher.png
old mode 100755
new mode 100644
index 4ec831a..fe216c6
Binary files a/app/src/main/res/drawable-mdpi/ic_launcher.png and b/app/src/main/res/drawable-mdpi/ic_launcher.png differ
diff --git a/app/src/main/res/drawable-xhdpi/ic_launcher.png b/app/src/main/res/drawable-xhdpi/ic_launcher.png
old mode 100755
new mode 100644
index c5d10eb..33303b3
Binary files a/app/src/main/res/drawable-xhdpi/ic_launcher.png and b/app/src/main/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/app/src/main/res/drawable-xxhdpi/ic_launcher.png b/app/src/main/res/drawable-xxhdpi/ic_launcher.png
old mode 100755
new mode 100644
index 9954998..532387a
Binary files a/app/src/main/res/drawable-xxhdpi/ic_launcher.png and b/app/src/main/res/drawable-xxhdpi/ic_launcher.png differ
diff --git a/app/src/main/res/drawable-xxxhdpi/ic_launcher.png b/app/src/main/res/drawable-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..b3d98b6
Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_launcher.png differ
diff --git a/app/src/main/res/drawable/button_berry.xml b/app/src/main/res/drawable/button_berry.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_black.xml b/app/src/main/res/drawable/button_black.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_blue.xml b/app/src/main/res/drawable/button_blue.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_brown.xml b/app/src/main/res/drawable/button_brown.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_cyan.xml b/app/src/main/res/drawable/button_cyan.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_elder.xml b/app/src/main/res/drawable/button_elder.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_green.xml b/app/src/main/res/drawable/button_green.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_grey.xml b/app/src/main/res/drawable/button_grey.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_olive.xml b/app/src/main/res/drawable/button_olive.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_orange.xml b/app/src/main/res/drawable/button_orange.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_pink.xml b/app/src/main/res/drawable/button_pink.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_purple.xml b/app/src/main/res/drawable/button_purple.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_red.xml b/app/src/main/res/drawable/button_red.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/drawable/button_yellow.xml b/app/src/main/res/drawable/button_yellow.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/ic_launcher-web.png b/app/src/main/res/ic_launcher-web.png
old mode 100755
new mode 100644
index ca36cd3..9edf183
Binary files a/app/src/main/res/ic_launcher-web.png and b/app/src/main/res/ic_launcher-web.png differ
diff --git a/app/src/main/res/icon.svg b/app/src/main/res/icon.svg
new file mode 100644
index 0000000..ef9d2ad
--- /dev/null
+++ b/app/src/main/res/icon.svg
@@ -0,0 +1,653 @@
+
+
+
+
diff --git a/app/src/main/res/layout-land/activity_main_d.xml b/app/src/main/res/layout-land/activity_main_d.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout-land/activity_main_g_k_r_t.xml b/app/src/main/res/layout-land/activity_main_g_k_r_t.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout-land/activity_main_i_m3.xml b/app/src/main/res/layout-land/activity_main_i_m3.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout-land/activity_main_m4.xml b/app/src/main/res/layout-land/activity_main_m4.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout-land/dialog_plugs.xml b/app/src/main/res/layout-land/dialog_plugs.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/activity_main_d.xml b/app/src/main/res/layout/activity_main_d.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/activity_main_g_k_r_t.xml b/app/src/main/res/layout/activity_main_g_k_r_t.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/activity_main_i_m3.xml b/app/src/main/res/layout/activity_main_i_m3.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/activity_main_kd.xml b/app/src/main/res/layout/activity_main_kd.xml
new file mode 100644
index 0000000..d2ecde5
--- /dev/null
+++ b/app/src/main/res/layout/activity_main_kd.xml
@@ -0,0 +1,170 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/layout/activity_main_m4.xml b/app/src/main/res/layout/activity_main_m4.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/dialog_about.xml b/app/src/main/res/layout/dialog_about.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/dialog_passphrase.xml b/app/src/main/res/layout/dialog_passphrase.xml
old mode 100755
new mode 100644
index daf8f43..c560ed0
--- a/app/src/main/res/layout/dialog_passphrase.xml
+++ b/app/src/main/res/layout/dialog_passphrase.xml
@@ -7,6 +7,6 @@
diff --git a/app/src/main/res/layout/dialog_plugs.xml b/app/src/main/res/layout/dialog_plugs.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/dialog_ringsettings_3.xml b/app/src/main/res/layout/dialog_ringsettings_3.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/dialog_ringsettings_4.xml b/app/src/main/res/layout/dialog_ringsettings_4.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/layout/dialog_two_options.xml b/app/src/main/res/layout/dialog_two_options.xml
new file mode 100644
index 0000000..2ae79f1
--- /dev/null
+++ b/app/src/main/res/layout/dialog_two_options.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/layout/dialog_whats_new.xml b/app/src/main/res/layout/dialog_whats_new.xml
new file mode 100644
index 0000000..b33ba1a
--- /dev/null
+++ b/app/src/main/res/layout/dialog_whats_new.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml
old mode 100755
new mode 100644
index 8cb6e13..eb29514
--- a/app/src/main/res/menu/main.xml
+++ b/app/src/main/res/menu/main.xml
@@ -1,51 +1,55 @@
diff --git a/app/src/main/res/render-icon.sh b/app/src/main/res/render-icon.sh
new file mode 100644
index 0000000..156efea
--- /dev/null
+++ b/app/src/main/res/render-icon.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+# Converts the Inkscape icon file ic_launcher_web.svg to the launcher web & app png files.
+
+PROJECT="/home/vanitas/Programmierung/androidstudio/EnigmAndroid/app"
+MAIN="${PROJECT}/src/main/"
+RES="${MAIN}res/"
+DRAWABLE="${RES}drawable"
+INPUT="${RES}icon.svg"
+
+inkscape --shell <Version
ZurücksetzenZufällige Konfiguration
- Lese Konfiguration aus QR-Code
- Teile Konfiguration per QR-Code
- Konfiguration aus Schlüsselwort
+ Konfiguration wiederherstellen
+ Konfiguration teilenEinstellungenRingstellungSenden
@@ -25,15 +24,23 @@
Position\nUmkehr-\nWalzePosition\nWalze 4Verkabelung Umkehrwalze
- Schlüsselwort eingeben
+ Schlüsselwort/KonfigurationVer-/EntschlüsselnNachricht ist leer.Fehler: Kein korrekter EnigmAndroid QR-Code!Ringstellungen
+
Steckbrett-\nVerbindungenVerkabelung Umkehrwalze
+
OKAbbrechen
+ Konfiguration wiederherstellen…
+ …aus Text/Schlüsselwort
+ …aus QR-code
+ Konfiguration teilen…
+ …per QR-code
+ …als kodierten TextSetze Ringe auf %1$s.Umkehrwalze verkabelt.Steckbrettverbindungen gesetzt.
@@ -42,5 +49,6 @@
Keine Änderungen.Enigma zurückgesetzt.Enigma auf zufällige Konfiguration gesetzt.
+ In Zwischenablage kopiert
diff --git a/app/src/main/res/values-de/strings_activity_settings.xml b/app/src/main/res/values-de/strings_activity_settings.xml
old mode 100755
new mode 100644
index db51763..512839f
--- a/app/src/main/res/values-de/strings_activity_settings.xml
+++ b/app/src/main/res/values-de/strings_activity_settings.xml
@@ -14,11 +14,12 @@
KSwiss-K (Schweiz)Swiss-K (Schweiz, Luftwaffe)
+ KDR (\"Rocket\", Reichsbahn)T (\"Tirpitz\", Japan)
- Eingabe Vorbereitung
+ KlartextvorbereitungZahlenbuchstabierspracheSprache in der Zahlen buchstabiert werden sollen.
diff --git a/app/src/main/res/values-de/strings_whatsnew.xml b/app/src/main/res/values-de/strings_whatsnew.xml
new file mode 100644
index 0000000..1e07276
--- /dev/null
+++ b/app/src/main/res/values-de/strings_whatsnew.xml
@@ -0,0 +1,4 @@
+
+
+ Was ist neu?
+
\ No newline at end of file
diff --git a/app/src/main/res/values-w820dp/dimens.xml b/app/src/main/res/values-w820dp/dimens.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/values/about_dialog_resources.xml b/app/src/main/res/values/about_dialog_resources.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
old mode 100755
new mode 100644
index 6d0e883..3fe3734
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -4,9 +4,8 @@
VersionResetRandom configuration
- Read configuration from QR-Code
- Share configuration via QR-Code
- Create configuration from passphrase
+ Restore configuration
+ Share configurationRing-SettingsSettingsSend
@@ -24,7 +23,7 @@
Position\nReflectorPosition\nRotor 4Wiring Reflector
- Enter passphrase
+ Passphrase/ConfigurationEn-/Decrypt!Can\'t send empty text.Error: Not a valid EnigmAndroid QR-Code!
@@ -33,6 +32,12 @@
Reflector WiringOKCancel
+ Restore configuration…
+ …from text
+ …from QR-code
+ Share configuration…
+ …as QR-code
+ …encoded in textSet Ring-Settings to %1$s.Rewired Reflector.Set Plugboard configuration.
@@ -41,8 +46,9 @@
No changes.Enigma reset.Enigma set to random configuration.
+ Copied to clipboard
- %1$s:%2$s
+ %1$s:%2$sI
diff --git a/app/src/main/res/values/strings_activity_settings.xml b/app/src/main/res/values/strings_activity_settings.xml
old mode 100755
new mode 100644
index 511a12d..33f88a0
--- a/app/src/main/res/values/strings_activity_settings.xml
+++ b/app/src/main/res/values/strings_activity_settings.xml
@@ -15,6 +15,7 @@
KSwiss-KSwiss-K (Airforce)
+ KDR (\"Rocket\", Railway)T (\"Tirpitz\", Japan)
@@ -29,6 +30,7 @@
KKSKSA
+ KDRT
diff --git a/app/src/main/res/values/strings_whatsnew.xml b/app/src/main/res/values/strings_whatsnew.xml
new file mode 100644
index 0000000..a3ea7b5
--- /dev/null
+++ b/app/src/main/res/values/strings_whatsnew.xml
@@ -0,0 +1,13 @@
+
+
+ What\'s new?
+ %1$s:
+
+ - This splash screen!
+ \n- Protocol-versioning to ensure compatibility to upcoming releases/new functions.\n
+ Unfortunately we had to break backwards compatibility to older versions :/
+ \n- Enigma KD!
+ \n- New Icon!
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml
old mode 100755
new mode 100644
diff --git a/app/src/main/res/xml/pref_page.xml b/app/src/main/res/xml/pref_page.xml
old mode 100755
new mode 100644
diff --git a/build.gradle b/build.gradle
old mode 100755
new mode 100644
index 9acae59..54ba3cd
--- a/build.gradle
+++ b/build.gradle
@@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:1.3.0'
+ classpath 'com.android.tools.build:gradle:2.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
diff --git a/build/generated/mockable-android-23.jar b/build/generated/mockable-android-23.jar
new file mode 100644
index 0000000..71d1286
Binary files /dev/null and b/build/generated/mockable-android-23.jar differ
diff --git a/build/intermediates/dex-cache/cache.xml b/build/intermediates/dex-cache/cache.xml
new file mode 100644
index 0000000..e57c535
--- /dev/null
+++ b/build/intermediates/dex-cache/cache.xml
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/build/intermediates/gradle_project_sync_data.bin b/build/intermediates/gradle_project_sync_data.bin
new file mode 100644
index 0000000..fbd5282
Binary files /dev/null and b/build/intermediates/gradle_project_sync_data.bin differ
diff --git a/build/intermediates/model_data.bin b/build/intermediates/model_data.bin
new file mode 100644
index 0000000..b5199d9
Binary files /dev/null and b/build/intermediates/model_data.bin differ
diff --git a/fdroid-badge.png b/fdroid-badge.png
new file mode 100644
index 0000000..6955af3
Binary files /dev/null and b/fdroid-badge.png differ
diff --git a/gradle.properties b/gradle.properties
old mode 100755
new mode 100644
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
old mode 100755
new mode 100644
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
old mode 100755
new mode 100644
index 0497007..5623dff
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Tue Feb 24 19:07:06 CET 2015
+#Fri Sep 09 18:16:07 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
diff --git a/gradlew b/gradlew
old mode 100755
new mode 100644
diff --git a/gradlew.bat b/gradlew.bat
old mode 100755
new mode 100644
diff --git a/settings.gradle b/settings.gradle
old mode 100755
new mode 100644