2015-09-30 14:25:49 +02:00
|
|
|
package de.vanitasvitae.enigmandroid.layout;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import de.vanitasvitae.enigmandroid.MainActivity;
|
|
|
|
import de.vanitasvitae.enigmandroid.R;
|
|
|
|
|
|
|
|
/**
|
2015-10-01 00:39:17 +02:00
|
|
|
* Builder for the dialog that is used to obtain a passphrase to generate
|
|
|
|
* a enigma configuration from it.
|
2015-10-09 00:57:54 +02:00
|
|
|
* Alternatively the user can enter the content String from a EnigmAndroid QR-Code here.
|
|
|
|
* That would have the same effect as scanning the QR-Code.
|
2015-09-30 14:25:49 +02:00
|
|
|
* Copyright (C) 2015 Paul Schaub
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
* @author vanitasvitae
|
|
|
|
*/
|
|
|
|
public class PassphraseDialogBuilder
|
|
|
|
{
|
2015-10-04 22:51:35 +02:00
|
|
|
private MainActivity main;
|
|
|
|
private View passphraseDialogView;
|
|
|
|
private EditText passphrase;
|
|
|
|
private Button positive;
|
2015-09-30 14:25:49 +02:00
|
|
|
public PassphraseDialogBuilder()
|
|
|
|
{
|
|
|
|
main = (MainActivity) MainActivity.ActivitySingleton.getInstance().getActivity();
|
|
|
|
passphraseDialogView = View.inflate(main, R.layout.dialog_passphrase, null);
|
|
|
|
passphrase = (EditText) passphraseDialogView.findViewById(R.id.passphrase);
|
|
|
|
passphrase.addTextChangedListener(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
2015-10-01 00:39:17 +02:00
|
|
|
//Do nothing
|
2015-09-30 14:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
2015-10-01 00:39:17 +02:00
|
|
|
//Do nothing
|
2015-09-30 14:25:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
2015-10-01 00:39:17 +02:00
|
|
|
//Count input text and enable positive button if length > 0.
|
|
|
|
//Disable else
|
2015-09-30 14:25:49 +02:00
|
|
|
if(s.length() > 0) positive.setEnabled(true);
|
|
|
|
else positive.setEnabled(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-10-01 00:39:17 +02:00
|
|
|
/**
|
|
|
|
* create and show the dialog
|
|
|
|
*/
|
2015-09-30 14:25:49 +02:00
|
|
|
public void showDialog()
|
|
|
|
{
|
|
|
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(main);
|
|
|
|
builder.setTitle(R.string.hint_passphrase);
|
|
|
|
Dialog d = builder.setView(passphraseDialogView)
|
|
|
|
.setCancelable(true)
|
2015-10-09 00:57:54 +02:00
|
|
|
.setPositiveButton(R.string.dialog_positive, new DialogInterface.OnClickListener()
|
2015-09-30 14:25:49 +02:00
|
|
|
{
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
String pass = passphrase.getText().toString();
|
2015-10-09 00:57:54 +02:00
|
|
|
if(pass.startsWith(MainActivity.APP_ID+"/"))
|
|
|
|
{
|
|
|
|
main.restoreStateFromCode(pass);
|
|
|
|
Toast.makeText(main, R.string.dialog_passphrase_was_coded_state, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
main.createStateFromSeed(pass);
|
|
|
|
String message = String.format(main.getResources().getString(
|
|
|
|
R.string.dialog_passphrase_set), " \'"+pass+"\'");
|
|
|
|
Toast.makeText(main, message, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2015-09-30 14:25:49 +02:00
|
|
|
}
|
|
|
|
})
|
2015-10-09 00:57:54 +02:00
|
|
|
.setNegativeButton(R.string.dialog_negative, new DialogInterface.OnClickListener() {
|
2015-09-30 14:25:49 +02:00
|
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
|
|
dialog.cancel();
|
|
|
|
Toast.makeText(main, R.string.dialog_abort,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
d.show();
|
|
|
|
positive = ((AlertDialog)d).getButton(AlertDialog.BUTTON_POSITIVE);
|
|
|
|
positive.setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|