2015-02-04 20:51:31 +01:00
|
|
|
package de.vanitasvitae.enigmandroid;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2015-02-18 21:51:40 +01:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.DialogInterface;
|
2015-02-24 22:04:02 +01:00
|
|
|
import android.content.Intent;
|
2015-02-24 23:36:24 +01:00
|
|
|
import android.content.SharedPreferences;
|
2015-08-27 01:48:33 +02:00
|
|
|
import android.content.res.Configuration;
|
2015-03-14 00:29:43 +01:00
|
|
|
import android.net.Uri;
|
2015-02-04 20:51:31 +01:00
|
|
|
import android.os.Bundle;
|
2015-02-24 23:36:24 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2015-09-15 02:06:47 +02:00
|
|
|
import android.util.Log;
|
2015-02-04 20:51:31 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2015-08-27 01:48:33 +02:00
|
|
|
import android.view.View;
|
2015-02-04 20:51:31 +01:00
|
|
|
import android.widget.Toast;
|
2015-08-27 01:48:33 +02:00
|
|
|
|
2015-09-30 14:25:49 +02:00
|
|
|
import com.google.zxing.integration.android.IntentIntegrator;
|
|
|
|
import com.google.zxing.integration.android.IntentResult;
|
2015-09-15 02:06:47 +02:00
|
|
|
|
2015-09-30 14:25:49 +02:00
|
|
|
import de.vanitasvitae.enigmandroid.enigma.Enigma;
|
|
|
|
import de.vanitasvitae.enigmandroid.enigma.EnigmaStateBundle;
|
2015-08-27 01:48:33 +02:00
|
|
|
import de.vanitasvitae.enigmandroid.enigma.inputPreparer.InputPreparer;
|
|
|
|
import de.vanitasvitae.enigmandroid.layout.LayoutContainer;
|
2015-09-30 14:25:49 +02:00
|
|
|
import de.vanitasvitae.enigmandroid.layout.PassphraseDialogBuilder;
|
2015-08-27 01:48:33 +02:00
|
|
|
|
2015-08-16 02:59:56 +02:00
|
|
|
/**
|
|
|
|
* Main Android Activity of the app
|
|
|
|
* Copyright (C) 2015 Paul Schaub
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2015-02-04 20:51:31 +01:00
|
|
|
|
2015-08-16 02:59:56 +02:00
|
|
|
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
|
|
|
|
*/
|
2015-02-04 20:51:31 +01:00
|
|
|
public class MainActivity extends Activity
|
|
|
|
{
|
2015-02-24 23:36:24 +01:00
|
|
|
private static final int RESULT_SETTINGS = 1;
|
2015-08-27 01:48:33 +02:00
|
|
|
private static final String URI_CHANGELOG =
|
|
|
|
"https://github.com/vanitasvitae/EnigmAndroid/blob/master/CHANGELOG.txt";
|
2015-09-30 14:25:49 +02:00
|
|
|
public static final String APP_ID = "EnigmAndroid";
|
2015-02-24 23:36:24 +01:00
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
LayoutContainer layoutContainer;
|
|
|
|
protected String prefMachineType;
|
|
|
|
protected boolean prefAnomaly;
|
|
|
|
protected String prefNumericLanguage;
|
2015-09-10 02:56:48 +02:00
|
|
|
protected String prefMessageFormatting;
|
2015-02-18 21:51:40 +01:00
|
|
|
|
2015-02-04 20:51:31 +01:00
|
|
|
@Override
|
2015-02-18 21:51:40 +01:00
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
2015-02-04 20:51:31 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
2015-08-16 02:59:56 +02:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-09-22 21:10:41 +02:00
|
|
|
this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
|
|
|
|
getStringArray(R.array.pref_alias_machine_type)[0]);
|
2015-08-15 03:53:02 +02:00
|
|
|
ActivitySingleton singleton = ActivitySingleton.getInstance();
|
|
|
|
singleton.setActivity(this);
|
2015-08-27 01:48:33 +02:00
|
|
|
updateContentView();
|
|
|
|
layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType);
|
|
|
|
updatePreferenceValues();
|
2015-08-15 03:53:02 +02:00
|
|
|
//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)
|
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
layoutContainer.getInput().setRawText(sharedText);
|
2015-08-15 03:53:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
super.onConfigurationChanged(newConfig);
|
2015-09-15 02:06:47 +02:00
|
|
|
this.updateContentView();
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void updateContentView()
|
|
|
|
{
|
|
|
|
switch (prefMachineType)
|
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
case "I":
|
|
|
|
this.setContentView(R.layout.activity_main_i_m3);
|
2015-08-27 01:48:33 +02:00
|
|
|
break;
|
|
|
|
case "M3":
|
|
|
|
this.setContentView(R.layout.activity_main_i_m3);
|
|
|
|
break;
|
2015-09-10 02:56:48 +02:00
|
|
|
case "M4":
|
|
|
|
this.setContentView(R.layout.activity_main_m4);
|
|
|
|
break;
|
|
|
|
case "D":
|
|
|
|
this.setContentView(R.layout.activity_main_d);
|
|
|
|
break;
|
2015-09-12 00:04:58 +02:00
|
|
|
case "K":
|
2015-09-16 14:38:46 +02:00
|
|
|
case "KS":
|
|
|
|
case "KSA":
|
2015-09-15 02:06:47 +02:00
|
|
|
case "T":
|
2015-09-16 14:38:46 +02:00
|
|
|
case "R":
|
|
|
|
case "G31":
|
|
|
|
case "G312":
|
|
|
|
case "G260":
|
|
|
|
this.setContentView(R.layout.activity_main_g_k_t);
|
2015-09-12 00:04:58 +02:00
|
|
|
break;
|
2015-08-27 01:48:33 +02:00
|
|
|
default:
|
|
|
|
this.setContentView(R.layout.activity_main_i_m3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePreferenceValues()
|
|
|
|
{
|
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-09-22 21:10:41 +02:00
|
|
|
this.setPrefMachineType(sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
|
|
|
|
getStringArray(R.array.pref_alias_machine_type)[0]));
|
|
|
|
this.setPrefAnomaly(sharedPreferences.getBoolean(SettingsActivity.PREF_ANOMALY, true));
|
|
|
|
this.setPrefNumericLanguage(sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources().
|
2015-08-27 01:48:33 +02:00
|
|
|
getStringArray(R.array.pref_alias_numeric_spelling_language)[0]));
|
2015-09-22 21:10:41 +02:00
|
|
|
this.setPrefMessageFormatting(sharedPreferences.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources().
|
2015-09-10 02:56:48 +02:00
|
|
|
getStringArray(R.array.pref_alias_message_formatting)[0]));
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setPrefMachineType(String type)
|
|
|
|
{
|
|
|
|
if(prefMachineType == null || !prefMachineType.equals(type))
|
|
|
|
{
|
|
|
|
prefMachineType = type;
|
|
|
|
String savedInput = "";
|
|
|
|
if(layoutContainer != null)
|
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
savedInput = layoutContainer.getInput().getText();
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
updateContentView();
|
|
|
|
layoutContainer = LayoutContainer.createLayoutContainer(prefMachineType);
|
2015-09-22 21:10:41 +02:00
|
|
|
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
|
2015-09-10 02:56:48 +02:00
|
|
|
layoutContainer.getInput().setText(savedInput);
|
2015-09-30 14:25:49 +02:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
sharedPreferences.edit().putString(SettingsActivity.PREF_MACHINE_TYPE, type);
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPrefMachineType()
|
|
|
|
{
|
|
|
|
if(prefMachineType != null) return prefMachineType;
|
2015-09-10 02:56:48 +02:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-09-22 21:10:41 +02:00
|
|
|
this.prefMachineType = sharedPreferences.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources().
|
|
|
|
getStringArray(R.array.pref_alias_machine_type)[0]);
|
2015-09-10 02:56:48 +02:00
|
|
|
return prefMachineType;
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setPrefAnomaly(boolean anomaly)
|
|
|
|
{
|
|
|
|
if(prefAnomaly !=anomaly)
|
|
|
|
{
|
|
|
|
prefAnomaly = anomaly;
|
2015-09-16 14:38:46 +02:00
|
|
|
if(layoutContainer != null && layoutContainer.getEnigma() != null) layoutContainer.getEnigma().setPrefAnomaly(anomaly);
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean getPrefAnomaly()
|
|
|
|
{
|
|
|
|
return prefAnomaly;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPrefNumericLanguage(String lang)
|
|
|
|
{
|
|
|
|
if(prefNumericLanguage == null || !prefNumericLanguage.equals(lang))
|
|
|
|
{
|
|
|
|
prefNumericLanguage = lang;
|
2015-09-22 21:10:41 +02:00
|
|
|
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPrefNumericLanguage()
|
|
|
|
{
|
|
|
|
if(prefNumericLanguage != null) return prefNumericLanguage;
|
2015-09-10 02:56:48 +02:00
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-09-22 21:10:41 +02:00
|
|
|
this.prefNumericLanguage = sharedPreferences.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources().
|
2015-09-10 02:56:48 +02:00
|
|
|
getStringArray(R.array.pref_alias_numeric_spelling_language)[0]);
|
|
|
|
return prefNumericLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPrefMessageFormatting(String messageFormatting)
|
|
|
|
{
|
|
|
|
if(prefMessageFormatting == null || !prefMessageFormatting.equals(messageFormatting))
|
2015-08-27 01:48:33 +02:00
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
prefMessageFormatting = messageFormatting;
|
|
|
|
layoutContainer.setEditTextAdapter(messageFormatting);
|
2015-08-27 01:48:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 02:56:48 +02:00
|
|
|
public String getPrefMessageFormatting()
|
|
|
|
{
|
|
|
|
if(prefMessageFormatting != null) return prefMessageFormatting;
|
|
|
|
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-09-22 21:10:41 +02:00
|
|
|
this.prefMessageFormatting = sharedPreferences.getString(SettingsActivity.PREF_MESSAGE_FORMATTING, getResources().
|
2015-09-10 02:56:48 +02:00
|
|
|
getStringArray(R.array.pref_alias_message_formatting)[0]);
|
|
|
|
return prefMessageFormatting;
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:25:49 +02:00
|
|
|
public void onDialogFinished(EnigmaStateBundle state)
|
|
|
|
{
|
|
|
|
layoutContainer.getEnigma().setState(state);
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:51:31 +01:00
|
|
|
@Override
|
2015-02-18 21:51:40 +01:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
2015-02-11 20:56:43 +01:00
|
|
|
this.getMenuInflater().inflate(R.menu.main, menu);
|
2015-02-04 20:51:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-02-18 21:51:40 +01:00
|
|
|
/**
|
|
|
|
* Handle Options menu clicks
|
|
|
|
*/
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item)
|
|
|
|
{
|
2015-02-04 20:51:31 +01:00
|
|
|
int id = item.getItemId();
|
2015-03-14 00:29:43 +01:00
|
|
|
if (id == R.id.action_reset)
|
2015-02-04 20:51:31 +01:00
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
layoutContainer.resetLayout();
|
2015-02-18 21:51:40 +01:00
|
|
|
Toast.makeText(getApplicationContext(), R.string.message_reset,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-26 16:04:02 +02:00
|
|
|
else if (id == R.id.action_random_configuration)
|
|
|
|
{
|
|
|
|
layoutContainer.getEnigma().randomState();
|
2015-09-30 14:25:49 +02:00
|
|
|
layoutContainer.syncStateFromEnigmaToLayout();
|
2015-09-26 16:04:02 +02:00
|
|
|
Toast.makeText(getApplicationContext(), R.string.message_random,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-30 14:25:49 +02:00
|
|
|
else if (id == R.id.action_choose_ringsetting)
|
2015-02-18 21:51:40 +01:00
|
|
|
{
|
2015-08-27 01:48:33 +02:00
|
|
|
layoutContainer.showRingSettingsDialog();
|
2015-02-18 21:51:40 +01:00
|
|
|
return true;
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
2015-02-24 22:04:02 +01:00
|
|
|
else if (id == R.id.action_settings)
|
|
|
|
{
|
|
|
|
Intent i = new Intent(this, SettingsActivity.class);
|
2015-02-24 23:36:24 +01:00
|
|
|
startActivityForResult(i, RESULT_SETTINGS);
|
2015-02-24 22:04:02 +01:00
|
|
|
}
|
2015-03-14 00:29:43 +01:00
|
|
|
else if (id == R.id.action_about)
|
|
|
|
{
|
|
|
|
showAboutDialog();
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-15 03:53:02 +02:00
|
|
|
else if (id == R.id.action_send)
|
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
if(layoutContainer.getOutput().getText().length() == 0)
|
2015-08-15 03:53:02 +02:00
|
|
|
{
|
2015-08-16 02:59:56 +02:00
|
|
|
Toast.makeText(this, R.string.error_no_text_to_send, Toast.LENGTH_SHORT).show();
|
2015-08-15 03:53:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Intent sendIntent = new Intent();
|
|
|
|
sendIntent.setAction(Intent.ACTION_SEND);
|
2015-09-10 02:56:48 +02:00
|
|
|
sendIntent.putExtra(Intent.EXTRA_TEXT, layoutContainer.getOutput().getModifiedText());
|
2015-08-15 03:53:02 +02:00
|
|
|
sendIntent.setType("text/plain");
|
|
|
|
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
|
|
|
|
}
|
|
|
|
}
|
2015-09-30 14:25:49 +02:00
|
|
|
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(layoutContainer.getEnigma().stateToString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(id == R.id.action_enter_seed)
|
|
|
|
{
|
|
|
|
new PassphraseDialogBuilder().showDialog();
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2015-02-18 21:51:40 +01:00
|
|
|
/**
|
2015-08-15 03:53:02 +02:00
|
|
|
* Set the chosen Configuration to the enigma, get the input string from the input textbox and
|
|
|
|
* prepare it, set the input to the prepared text, encrypt the prepared input and set the
|
|
|
|
* encrypted string to the output textbox and update the spinners to their new positions.
|
2015-02-18 21:51:40 +01:00
|
|
|
* @param v View
|
|
|
|
*/
|
|
|
|
public void doCrypto(View v)
|
2015-02-04 20:51:31 +01:00
|
|
|
{
|
2015-09-10 02:56:48 +02:00
|
|
|
layoutContainer.doCrypto();
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|
|
|
|
|
2015-02-18 21:51:40 +01:00
|
|
|
/**
|
2015-08-27 01:48:33 +02:00
|
|
|
* Show a Dialog containing information about the app, license, usage, author and a link
|
|
|
|
* to the changelog
|
2015-02-18 21:51:40 +01:00
|
|
|
*/
|
2015-03-14 00:29:43 +01:00
|
|
|
public void showAboutDialog()
|
|
|
|
{
|
|
|
|
final View aboutView = View.inflate(this, R.layout.dialog_about, null);
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setTitle(R.string.title_about_dialog);
|
|
|
|
builder.setView(aboutView)
|
|
|
|
.setCancelable(true)
|
|
|
|
.setPositiveButton(R.string.dialog_positiv, new DialogInterface.OnClickListener()
|
|
|
|
{
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.button_show_changelog, new DialogInterface.OnClickListener()
|
|
|
|
{
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
dialog.cancel();
|
|
|
|
openWebPage(URI_CHANGELOG);
|
|
|
|
}
|
|
|
|
}).show();
|
|
|
|
}
|
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
/**
|
|
|
|
* Handle preference changes
|
|
|
|
* @param requestCode requestCode
|
|
|
|
* @param resultCode resultCode (RESULT_SETTINGS is defined at the top)
|
|
|
|
* @param data data (not important here)
|
|
|
|
*/
|
2015-02-24 23:36:24 +01:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
switch (requestCode) {
|
|
|
|
case RESULT_SETTINGS:
|
|
|
|
{
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2015-09-22 21:10:41 +02:00
|
|
|
this.setPrefMachineType(sharedPrefs.getString(SettingsActivity.PREF_MACHINE_TYPE, getResources()
|
|
|
|
.getStringArray(R.array.pref_alias_machine_type)[0]));
|
|
|
|
this.setPrefAnomaly(sharedPrefs.getBoolean(SettingsActivity.PREF_ANOMALY, true));
|
|
|
|
this.setPrefNumericLanguage(sharedPrefs.getString(SettingsActivity.PREF_NUMERIC_LANGUAGE, getResources().
|
2015-08-27 01:48:33 +02:00
|
|
|
getStringArray(R.array.pref_alias_numeric_spelling_language)[0]));
|
2015-09-22 21:10:41 +02:00
|
|
|
this.setPrefMessageFormatting(sharedPrefs.getString(SettingsActivity.PREF_MESSAGE_FORMATTING,
|
2015-09-10 02:56:48 +02:00
|
|
|
getResources().getStringArray(R.array.pref_alias_message_formatting)[0]));
|
2015-02-24 23:36:24 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-09-30 14:25:49 +02:00
|
|
|
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);
|
|
|
|
}
|
2015-02-24 23:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-14 00:29:43 +01:00
|
|
|
|
2015-10-01 00:39:17 +02:00
|
|
|
/**
|
|
|
|
* Set EnigmAndroid into a certain state as described in the QR-Code
|
|
|
|
* @param mem content of the QR-Code
|
|
|
|
*/
|
2015-09-30 14:25:49 +02:00
|
|
|
private void restoreStateFromCode(String mem)
|
|
|
|
{
|
2015-10-01 00:39:17 +02:00
|
|
|
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());
|
|
|
|
setPrefMachineType(Enigma.chooseEnigmaFromSave(mem));
|
|
|
|
updateContentView();
|
|
|
|
layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType());
|
|
|
|
layoutContainer.getEnigma().restoreState(mem);
|
|
|
|
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
|
|
|
|
layoutContainer.syncStateFromEnigmaToLayout();
|
|
|
|
}
|
2015-09-30 14:25:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-01 00:39:17 +02:00
|
|
|
/**
|
|
|
|
* Set EnigmAndroid into a state calculated from the seed.
|
|
|
|
* @param seed seed
|
|
|
|
*/
|
2015-09-30 14:25:49 +02:00
|
|
|
public void createStateFromSeed(String seed)
|
|
|
|
{
|
|
|
|
setPrefMachineType(Enigma.chooseEnigmaFromSeed(seed));
|
|
|
|
updateContentView();
|
|
|
|
layoutContainer = LayoutContainer.createLayoutContainer(getPrefMachineType());
|
|
|
|
layoutContainer.getEnigma().setStateFromSeed(seed);
|
|
|
|
layoutContainer.setInputPreparer(InputPreparer.createInputPreparer());
|
|
|
|
layoutContainer.syncStateFromEnigmaToLayout();
|
|
|
|
}
|
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
/**
|
|
|
|
* Open the web page with the URL url
|
|
|
|
* @param url URL of the website
|
|
|
|
*/
|
2015-03-14 00:29:43 +01:00
|
|
|
public void openWebPage(String url) {
|
2015-08-27 01:48:33 +02:00
|
|
|
Uri webPage = Uri.parse(url);
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW, webPage);
|
2015-03-14 00:29:43 +01:00
|
|
|
if (intent.resolveActivity(getPackageManager()) != null) {
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
2015-08-15 03:53:02 +02:00
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
/**
|
|
|
|
* Singleton that grants access to an Activity from anywhere within the app
|
|
|
|
*/
|
|
|
|
public static class ActivitySingleton
|
|
|
|
{
|
|
|
|
private static ActivitySingleton instance = null;
|
|
|
|
private Activity activity;
|
2015-08-15 03:53:02 +02:00
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
//private constructor
|
|
|
|
private ActivitySingleton(){}
|
|
|
|
//Singleton method
|
|
|
|
public static ActivitySingleton getInstance()
|
|
|
|
{
|
|
|
|
if(instance == null) instance = new ActivitySingleton();
|
|
|
|
return instance;
|
|
|
|
}
|
2015-08-15 03:53:02 +02:00
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
/**
|
|
|
|
* Set an Activity that the Singleton returns
|
|
|
|
* @param activity activity that's stored
|
|
|
|
*/
|
|
|
|
public void setActivity(Activity activity)
|
|
|
|
{
|
|
|
|
this.activity = activity;
|
|
|
|
}
|
2015-08-15 03:53:02 +02:00
|
|
|
|
2015-08-27 01:48:33 +02:00
|
|
|
/**
|
|
|
|
* Returns the stored Activity
|
|
|
|
* @return stored Activity
|
|
|
|
*/
|
|
|
|
public Activity getActivity()
|
|
|
|
{
|
|
|
|
return activity;
|
2015-08-15 03:53:02 +02:00
|
|
|
}
|
2015-08-27 01:48:33 +02:00
|
|
|
|
|
|
|
}
|
2015-02-04 20:51:31 +01:00
|
|
|
}
|