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

@ -68,19 +68,20 @@ public class MainActivity extends Activity
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
secureRandom = new SecureRandom(); secureRandom = new SecureRandom();
ActivitySingleton singleton = ActivitySingleton.getInstance();
singleton.setActivity(this);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources(). this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
getStringArray(R.array.pref_alias_machine_type)[0]); getStringArray(R.array.pref_alias_machine_type)[0]);
ActivitySingleton singleton = ActivitySingleton.getInstance();
singleton.setActivity(this);
updateContentView(); updateContentView();
layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType); layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType);
updatePreferenceValues(); updatePreferenceValues();
//Handle shared text //Handle shared text
Intent intent = getIntent(); Intent intent = getIntent();
String action = intent.getAction(); String action = intent.getAction();
String type = intent.getType(); String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) { if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) if ("text/plain".equals(type))
{ {
@ -243,6 +244,7 @@ public class MainActivity extends Activity
layoutContainer.syncStateFromEnigmaToLayout(); layoutContainer.syncStateFromEnigmaToLayout();
Toast.makeText(getApplicationContext(), R.string.message_random, Toast.makeText(getApplicationContext(), R.string.message_random,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
layoutContainer.getOutput().setText("");
return true; return true;
} }
else if (id == R.id.action_choose_ringsetting) else if (id == R.id.action_choose_ringsetting)
@ -319,6 +321,7 @@ public class MainActivity extends Activity
PackageInfo pInfo = null; PackageInfo pInfo = null;
try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);} try{ pInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);}
catch (PackageManager.NameNotFoundException e){ e.printStackTrace();} catch (PackageManager.NameNotFoundException e){ e.printStackTrace();}
assert pInfo != null;
String version = pInfo.versionName+ " ("+pInfo.versionCode+")"; String version = pInfo.versionName+ " ("+pInfo.versionCode+")";
TextView versionText = (TextView) aboutView.findViewById(R.id.about_version_section); TextView versionText = (TextView) aboutView.findViewById(R.id.about_version_section);
versionText.setText(version); versionText.setText(version);
@ -391,6 +394,7 @@ public class MainActivity extends Activity
} }
else else
{ {
String inputString = layoutContainer.getInput().getText();
mem = mem.substring((APP_ID+"/").length()); mem = mem.substring((APP_ID+"/").length());
BigInteger s = new BigInteger(mem, 16); BigInteger s = new BigInteger(mem, 16);
Log.d(APP_ID, "Try to restore configuration from BigInteger value "+ s.toString()); Log.d(APP_ID, "Try to restore configuration from BigInteger value "+ s.toString());
@ -400,6 +404,8 @@ public class MainActivity extends Activity
layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20)); layoutContainer.getEnigma().restoreState(Enigma.removeDigit(s,20));
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
layoutContainer.syncStateFromEnigmaToLayout(); layoutContainer.syncStateFromEnigmaToLayout();
layoutContainer.getInput().setText(inputString);
layoutContainer.getOutput().setText("");
} }
} }
@ -409,12 +415,15 @@ public class MainActivity extends Activity
*/ */
public void createStateFromSeed(String seed) public void createStateFromSeed(String seed)
{ {
String inputString = layoutContainer.getInput().getText();
setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed)); setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed));
updateContentView(); updateContentView();
layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType()); layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType());
layoutContainer.getEnigma().setStateFromSeed(seed); layoutContainer.getEnigma().setStateFromSeed(seed);
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer()); layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
layoutContainer.syncStateFromEnigmaToLayout(); layoutContainer.syncStateFromEnigmaToLayout();
layoutContainer.getInput().setText(inputString);
layoutContainer.getOutput().setText("");
} }
/** /**

View file

@ -2,6 +2,7 @@ package de.vanitasvitae.enigmandroid;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
/** /**
* Class that represents the settings activity. * 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_NUMERIC_LANGUAGE = "prefNumericLanguage";
public static final String PREF_MACHINE_TYPE = "prefMachineType"; 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_MESSAGE_FORMATTING = "prefMessageFormatting";
public static final String PREF_REPLACE_SPECIAL_CHARACTERS = "prefReplaceSpecialCharacters"; 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 @Override
protected void onCreate(Bundle savedInstanceState) protected void onCreate(Bundle savedInstanceState)
@ -37,5 +43,143 @@ public class SettingsActivity extends PreferenceActivity
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//noinspection deprecation //noinspection deprecation
addPreferencesFromResource(R.xml.pref_page); 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.MainActivity;
import de.vanitasvitae.enigmandroid.R; import de.vanitasvitae.enigmandroid.R;
import de.vanitasvitae.enigmandroid.SettingsActivity;
import de.vanitasvitae.enigmandroid.enigma.Enigma; import de.vanitasvitae.enigmandroid.enigma.Enigma;
import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle; import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle;
import de.vanitasvitae.enigmandroid.enigma.inputPreparer.EditTextAdapter; import de.vanitasvitae.enigmandroid.enigma.inputPreparer.EditTextAdapter;
@ -60,6 +61,7 @@ public abstract class LayoutContainer
this.outputView = (EditText) main.findViewById(R.id.output); this.outputView = (EditText) main.findViewById(R.id.output);
input = EditTextAdapter.createEditTextAdapter(inputView, main.getPrefMessageFormatting()); input = EditTextAdapter.createEditTextAdapter(inputView, main.getPrefMessageFormatting());
output = EditTextAdapter.createEditTextAdapter(outputView, main.getPrefMessageFormatting()); output = EditTextAdapter.createEditTextAdapter(outputView, main.getPrefMessageFormatting());
inputPreparer = InputPreparer.createInputPreparer();
initializeLayout(); initializeLayout();
} }
@ -86,6 +88,11 @@ public abstract class LayoutContainer
return this.output; return this.output;
} }
public static LayoutContainer createLayoutContainer()
{
return createLayoutContainer(SettingsActivity.SettingsSingleton.getInstance().getPrefMachineType());
}
public static LayoutContainer createLayoutContainer(String enigmaType) public static LayoutContainer createLayoutContainer(String enigmaType)
{ {
switch (enigmaType) { switch (enigmaType) {

View file

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

View file

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

View file

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

View file

@ -29,7 +29,7 @@ import de.vanitasvitae.enigmandroid.enigma.Enigma_K;
*/ */
public class LayoutContainer_K extends LayoutContainer public class LayoutContainer_K extends LayoutContainer
{ {
private Enigma_K enigma; protected Enigma enigma;
protected Spinner rotor1View; protected Spinner rotor1View;
protected Spinner rotor2View; protected Spinner rotor2View;
@ -43,6 +43,7 @@ public class LayoutContainer_K extends LayoutContainer
public LayoutContainer_K() public LayoutContainer_K()
{ {
super(); super();
main.setContentView(R.layout.activity_main_g_k_r_t);
main.setTitle("K - EnigmAndroid"); main.setTitle("K - EnigmAndroid");
this.resetLayout(); 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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author vanitasvitae * @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() public LayoutContainer_K_Swiss()
{ {
super(); super();
@ -47,34 +36,6 @@ public class LayoutContainer_K_Swiss extends LayoutContainer
this.resetLayout(); 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 @Override
public void resetLayout() { public void resetLayout() {
enigma = new Enigma_K_Swiss_Standard(); enigma = new Enigma_K_Swiss_Standard();
@ -82,37 +43,4 @@ public class LayoutContainer_K_Swiss extends LayoutContainer
output.setText(""); output.setText("");
input.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. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author vanitasvitae * @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() public LayoutContainer_K_Swiss_Airforce()
{ {
super(); super();
@ -47,34 +36,6 @@ public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer
this.resetLayout(); 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 @Override
public void resetLayout() { public void resetLayout() {
enigma = new Enigma_K_Swiss_Airforce(); enigma = new Enigma_K_Swiss_Airforce();
@ -82,37 +43,4 @@ public class LayoutContainer_K_Swiss_Airforce extends LayoutContainer
output.setText(""); output.setText("");
input.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() public LayoutContainer_M4()
{ {
super(); super();
main.setContentView(R.layout.activity_main_m4);
main.setTitle("M4 - EnigmAndroid"); main.setTitle("M4 - EnigmAndroid");
this.resetLayout(); this.resetLayout();
} }

View file

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

View file

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