Started new Iteration of MainActivity as Main2, moved getters and setters of preferences to SettingsActivity

This commit is contained in:
VanitasVitae 2015-10-11 13:51:25 +02:00
parent d1d3211cca
commit ba4a0f590f
13 changed files with 890 additions and 531 deletions

View file

@ -0,0 +1,336 @@
package de.vanitasvitae.enigmandroid;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.math.BigInteger;
import java.security.SecureRandom;
import de.vanitasvitae.enigmandroid.enigma.Enigma;
import de.vanitasvitae.enigmandroid.layout.LayoutContainer;
import de.vanitasvitae.enigmandroid.layout.PassphraseDialogBuilder;
/**
* Reimplementation of MainActivity
* Created by vanitas on 11.10.15.
*/
public class Main2 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 LayoutContainer layoutContainer;
private SecureRandom numberGenerator;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.numberGenerator = new SecureRandom();
MainActivity.ActivitySingleton singleton = MainActivity.ActivitySingleton.getInstance();
singleton.setActivity(this);
numberGenerator = new SecureRandom();
restoreEnigmaModelAndState();
//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)
{
layoutContainer.getInput().setRawText(sharedText);
}
}
}
}
@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_send)
{
actionShareMessage();
}
else if (id == R.id.action_choose_ringsetting)
{
layoutContainer.showRingSettingsDialog();
return true;
}
else if(id == R.id.action_enter_seed)
{
new PassphraseDialogBuilder().showDialog();
return true;
}
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)
{
actionShareConfiguration();
return true;
}
else if (id == R.id.action_random_configuration)
{
actionRandomConfiguration();
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)
{
actionAbout();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Handle preference changes
* @param requestCode requestCode
* @param resultCode resultCode (RESULT_SETTINGS is defined at the top)
* @param data data (not important here)
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//Settings
case RESULT_SETTINGS:
{
restoreEnigmaModelAndState();
break;
}
//QR_Scanner
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!");
restoreEnigmaModelAndState(content);
}
}
}
}
@Override
protected void onPause()
{
super.onPause();
//TODO: Save state
}
private boolean restoreEnigmaModelAndState()
{
String savedState = SettingsActivity.SettingsSingleton.getInstance().getPrefSavedEnigmaState();
if (savedState.equals("-1") || SettingsActivity.SettingsSingleton.getInstance().prefSavedEnigmaStateChanged())
{
layoutContainer = LayoutContainer.createLayoutContainer(
SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType());
return false;
}
//No changes
return false;
}
private boolean restoreEnigmaModelAndState(BigInteger savedState)
{
SettingsActivity.SettingsSingleton.getInstance().setPrefMachineType(
Enigma.chooseEnigmaFromSave(savedState));
String savedInput = "";
if(layoutContainer != null) savedInput = layoutContainer.getInput().getText();
layoutContainer = LayoutContainer.createLayoutContainer(
SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType());
layoutContainer.getEnigma().restoreState(Enigma.removeDigit(savedState, 20));
layoutContainer.syncStateFromEnigmaToLayout();
layoutContainer.getInput().setText(savedInput);
layoutContainer.getOutput().setText("");
return true;
}
/**
* Restore Enigma state from String. String has to start with "EnigmAndroid/"
* @param savedState String
* @return success
*/
private boolean restoreEnigmaModelAndState(String savedState)
{
if(!savedState.startsWith(APP_ID+"/"))
{
Toast.makeText(this, R.string.error_no_valid_qr, Toast.LENGTH_LONG).show();
return false;
}
else
{
savedState = savedState.substring((APP_ID+"/").length());
BigInteger s = new BigInteger(savedState, 16);
return restoreEnigmaModelAndState(s);
}
}
/**
* Set EnigmAndroid into a state calculated from the seed.
* @param seed seed
*/
public void createStateFromSeed(String seed)
{
String savedInput = "";
if(layoutContainer != null) savedInput = layoutContainer.getInput().getText();
SettingsActivity.SettingsSingleton.getInstance().setPrefMachineType(
Enigma.chooseEnigmaFromSeed(seed));
layoutContainer = LayoutContainer.createLayoutContainer();
layoutContainer.getEnigma().setStateFromSeed(seed);
layoutContainer.syncStateFromEnigmaToLayout();
layoutContainer.getInput().setText(savedInput);
layoutContainer.getOutput().setText("");
}
/**
* 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();
}
/**
* If there is any message inside the right text field, share it via intent.
* Otherwise show a Toast.
*/
private void actionShareMessage()
{
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)));
}
}
/**
* Share current enigma configuration via QR-Code
*/
private void actionShareConfiguration()
{
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());
}
/**
* Set the enigma to a random state.
* Do not change enigma model.
*/
private void actionRandomConfiguration()
{
layoutContainer.getEnigma().randomState();
layoutContainer.syncStateFromEnigmaToLayout();
Toast.makeText(getApplicationContext(), R.string.message_random,
Toast.LENGTH_SHORT).show();
layoutContainer.getOutput().setText("");
}
/**
* Show the credits dialog of the app
*/
private void actionAbout()
{
final View aboutView = View.inflate(this, R.layout.dialog_about, null);
//Get and set Version code
PackageInfo pInfo = null;
try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);}
catch (PackageManager.NameNotFoundException e){ e.printStackTrace();}
assert pInfo != null;
String version = pInfo.versionName+ " ("+pInfo.versionCode+")";
TextView versionText = (TextView) aboutView.findViewById(R.id.about_version_section);
versionText.setText(version);
//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();
Uri webPage = Uri.parse(URI_CHANGELOG);
Intent intent = new Intent(Intent.ACTION_VIEW, webPage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}).show();
}
public SecureRandom getNumberGenerator()
{
return this.numberGenerator;
}
}

View file

@ -51,418 +51,427 @@ 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";
private LayoutContainer layoutContainer;
private String prefMachineType;
private String prefNumericLanguage;
private String prefMessageFormatting;
private LayoutContainer layoutContainer;
private String prefMachineType;
private String prefNumericLanguage;
private String prefMessageFormatting;
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 singleton = ActivitySingleton.getInstance();
singleton.setActivity(this);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
getStringArray(R.array.pref_alias_machine_type)[0]);
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);
}
}
}
}
updateContentView();
layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType);
updatePreferenceValues();
@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)
{
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;
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
this.updateContentView();
}
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]));
}
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;
}
}
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();
}
}
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 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;
}
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();
}
}
public void setPrefNumericLanguage(String lang)
{
if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang))
{
prefNumericLanguage = lang;
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
}
}
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;
}
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;
}
public void setPrefNumericLanguage(String lang)
{
if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang))
{
prefNumericLanguage = lang;
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
}
}
public void setPrefMessageFormatting(String messageFormatting)
{
if(prefMessageFormatting == null || !prefMessageFormatting.equals(messageFormatting))
{
prefMessageFormatting = messageFormatting;
layoutContainer.setEditTextAdapter(messageFormatting);
}
}
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;
}
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;
}
public void setPrefMessageFormatting(String messageFormatting)
{
if(prefMessageFormatting == null || !prefMessageFormatting.equals(messageFormatting))
{
prefMessageFormatting = messageFormatting;
layoutContainer.setEditTextAdapter(messageFormatting);
}
}
public SecureRandom getSecureRandom()
{
return this.secureRandom;
}
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;
}
public void onDialogFinished(EnigmaStateBundle state)
{
layoutContainer.getEnigma().setState(state);
}
public SecureRandom getSecureRandom()
{
return this.secureRandom;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onDialogFinished(EnigmaStateBundle state)
{
layoutContainer.getEnigma().setState(state);
}
@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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 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();
}
@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();
layoutContainer.getOutput().setText("");
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);
}
/**
* 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);
/**
* 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);
//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);
/**
* Handle preference changes
* @param requestCode requestCode
* @param resultCode resultCode (RESULT_SETTINGS is defined at the top)
* @param data data (not important here)
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SETTINGS:
{
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
this.setPrefMachineType(sharedPrefs.getString(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);
}
}
}
}
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);
}
}
}
}
/**
* 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();
}
}
/**
* 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);
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();
layoutContainer.getInput().setText(inputString);
layoutContainer.getOutput().setText("");
}
}
/**
* 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();
}
/**
* Set EnigmAndroid into a state calculated from the seed.
* @param seed seed
*/
public void createStateFromSeed(String seed)
{
String inputString = layoutContainer.getInput().getText();
setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed));
updateContentView();
layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType());
layoutContainer.getEnigma().setStateFromSeed(seed);
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
layoutContainer.syncStateFromEnigmaToLayout();
layoutContainer.getInput().setText(inputString);
layoutContainer.getOutput().setText("");
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* Singleton that grants access to an Activity from anywhere within the app
*/
public static class ActivitySingleton
{
private static ActivitySingleton instance = null;
private 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;
//private constructor
private ActivitySingleton(){}
//Singleton method
public static ActivitySingleton getInstance()
{
if(instance == null) instance = new ActivitySingleton();
return instance;
}
//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;
}
/**
* 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;
}
/**
* Returns the stored Activity
* @return stored Activity
*/
public Activity getActivity()
{
return activity;
}
}
}
}

View file

@ -2,6 +2,7 @@ package de.vanitasvitae.enigmandroid;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
/**
* Class that represents the settings activity.
@ -26,10 +27,15 @@ 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_SAVED_ENIGMA_STATE = "prefSavedEnigmaState";
private String previousPrefNumericLanguage;
private String previousPrefMachineType;
private String previousPrefMessageFormatting;
private boolean previousPrefReplaceSpecialCharacters;
private String previousPrefSavedEnigmaState;
@Override
protected void onCreate(Bundle savedInstanceState)
@ -37,5 +43,143 @@ public class SettingsActivity extends PreferenceActivity
super.onCreate(savedInstanceState);
//noinspection deprecation
addPreferencesFromResource(R.xml.pref_page);
this.previousPrefMachineType = getPrefMachineType();
this.previousPrefSavedEnigmaState = getPrefSavedEnigmaState();
this.previousPrefMessageFormatting = getPrefMessageFormatting();
this.previousPrefNumericLanguage = getPrefNumericLanguage();
}
public String getPrefNumericLanguage()
{
return PreferenceManager.getDefaultSharedPreferences(this).getString(
PREF_NUMERIC_LANGUAGE,
getResources().getStringArray(R.array.pref_alias_message_formatting)[0]);
}
public void setPrefNumericLanguage(String lang)
{
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString(PREF_NUMERIC_LANGUAGE, lang).apply();
}
public boolean prefNumericLanguageChanged()
{
if(this.previousPrefNumericLanguage == null || !this.previousPrefNumericLanguage.equals(getPrefNumericLanguage()))
{
this.previousPrefNumericLanguage = this.getPrefNumericLanguage();
return true;
}
return false;
}
public boolean getPrefReplaceSpecialCharacters()
{
return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(
PREF_REPLACE_SPECIAL_CHARACTERS, true);
}
public void setPrefReplaceSpecialCharacters(boolean replace)
{
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putBoolean(PREF_REPLACE_SPECIAL_CHARACTERS, replace).apply();
}
public boolean prefReplaceSpecialCharactersChanged()
{
boolean changed = previousPrefReplaceSpecialCharacters != getPrefReplaceSpecialCharacters();
if(changed)
{
previousPrefReplaceSpecialCharacters = getPrefReplaceSpecialCharacters();
return true;
}
return false;
}
public String getPrefMachineType()
{
return PreferenceManager.getDefaultSharedPreferences(this).getString(
PREF_MACHINE_TYPE,
getResources().getStringArray(R.array.pref_alias_machine_type)[0]);
}
public void setPrefMachineType(String pref)
{
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString(PREF_MACHINE_TYPE, pref).apply();
}
public boolean prefMachineTypeChanged()
{
if(this.previousPrefMachineType == null || !this.previousPrefMachineType.equals(getPrefMachineType()))
{
this.previousPrefMachineType = this.getPrefMachineType();
return true;
}
return false;
}
public String getPrefSavedEnigmaState()
{
return PreferenceManager.getDefaultSharedPreferences(this)
.getString(PREF_SAVED_ENIGMA_STATE, "-1");
}
/**
* @param state HEX
*/
public void setPrefSavedEnigmaState(String state)
{
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString(PREF_SAVED_ENIGMA_STATE, state).apply();
}
public boolean prefSavedEnigmaStateChanged()
{
if(this.previousPrefSavedEnigmaState == null || !this.previousPrefSavedEnigmaState
.equals(getPrefSavedEnigmaState()))
{
this.previousPrefSavedEnigmaState = this.getPrefSavedEnigmaState();
return true;
}
return false;
}
public String getPrefMessageFormatting()
{
return PreferenceManager.getDefaultSharedPreferences(this)
.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources().
getStringArray(R.array.pref_alias_message_formatting)[0]);
}
public void setPrefMessageFormatting(String format)
{
PreferenceManager.getDefaultSharedPreferences(this).edit()
.putString(PREF_MESSAGE_FORMATTING, format).apply();
}
public boolean prefMessageFormattingChanged()
{
if(this.previousPrefMessageFormatting == null || !this.previousPrefMessageFormatting
.equals(getPrefMessageFormatting()))
{
this.previousPrefMessageFormatting = this.getPrefMessageFormatting();
return true;
}
return false;
}
public static class SettingsSingleton extends SettingsActivity
{
private static SettingsActivity instance;
private SettingsSingleton()
{
super();
}
public static SettingsActivity getInstance()
{
if(instance == null) instance = new SettingsActivity();
return instance;
}
}
}

View file

@ -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;
@ -60,6 +61,7 @@ public abstract class LayoutContainer
this.outputView = (EditText) main.findViewById(R.id.output);
input = EditTextAdapter.createEditTextAdapter(inputView, main.getPrefMessageFormatting());
output = EditTextAdapter.createEditTextAdapter(outputView, main.getPrefMessageFormatting());
inputPreparer = InputPreparer.createInputPreparer();
initializeLayout();
}
@ -86,6 +88,11 @@ public abstract class LayoutContainer
return this.output;
}
public static LayoutContainer createLayoutContainer()
{
return createLayoutContainer(SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType());
}
public static LayoutContainer createLayoutContainer(String enigmaType)
{
switch (enigmaType) {

View file

@ -40,6 +40,7 @@ public class LayoutContainer_D extends LayoutContainer
public LayoutContainer_D()
{
super();
main.setContentView(R.layout.activity_main_d);
main.setTitle("D - EnigmAndroid");
this.resetLayout();
}

View file

@ -43,6 +43,7 @@ public class LayoutContainer_G31 extends LayoutContainer
public LayoutContainer_G31()
{
super();
main.setContentView(R.layout.activity_main_g_k_r_t);
main.setTitle("G31 - EnigmAndroid");
this.resetLayout();
}

View file

@ -43,6 +43,7 @@ public class LayoutContainer_I extends LayoutContainer
public LayoutContainer_I()
{
super();
main.setContentView(R.layout.activity_main_i_m3);
main.setTitle("I - EnigmAndroid");
this.resetLayout();
}

View file

@ -29,7 +29,7 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_K;
*/
public class LayoutContainer_K extends LayoutContainer
{
private Enigma_K enigma;
protected Enigma enigma;
protected Spinner rotor1View;
protected Spinner rotor2View;
@ -43,6 +43,7 @@ public class LayoutContainer_K extends LayoutContainer
public LayoutContainer_K()
{
super();
main.setContentView(R.layout.activity_main_g_k_r_t);
main.setTitle("K - EnigmAndroid");
this.resetLayout();
}

View file

@ -27,19 +27,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 +36,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 +43,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());
}
}

View file

@ -27,19 +27,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 +36,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 +43,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());
}
}

View file

@ -45,6 +45,7 @@ public class LayoutContainer_M4 extends LayoutContainer
public LayoutContainer_M4()
{
super();
main.setContentView(R.layout.activity_main_m4);
main.setTitle("M4 - EnigmAndroid");
this.resetLayout();
}

View file

@ -43,6 +43,7 @@ public class LayoutContainer_R extends LayoutContainer
public LayoutContainer_R()
{
super();
main.setContentView(R.layout.activity_main_g_k_r_t);
main.setTitle("R - EnigmAndroid");
this.resetLayout();
}

View file

@ -43,6 +43,7 @@ public class LayoutContainer_T extends LayoutContainer
public LayoutContainer_T()
{
super();
main.setContentView(R.layout.activity_main_g_k_r_t);
main.setTitle("T - EnigmAndroid");
this.resetLayout();
}