session-android/src/org/thoughtcrime/securesms/PassphraseCreateActivity.java

136 lines
4.7 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 18:20:44 +00:00
* 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 3 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.
*
2011-12-20 18:20:44 +00:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.thoughtcrime.securesms;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.util.MemoryCleaner;
2011-12-20 18:20:44 +00:00
/**
* Activity for creating a user's local encryption passphrase.
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*/
public class PassphraseCreateActivity extends PassphraseActivity {
private EditText passphraseEdit;
private EditText passphraseRepeatEdit;
private Button okButton;
private Button cancelButton;
2011-12-20 18:20:44 +00:00
public PassphraseCreateActivity() { }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2011-12-20 18:20:44 +00:00
setContentView(R.layout.create_passphrase_activity);
2011-12-20 18:20:44 +00:00
initializeResources();
}
2011-12-20 18:20:44 +00:00
private void initializeResources() {
this.passphraseEdit = (EditText) findViewById(R.id.passphrase_edit);
this.passphraseRepeatEdit = (EditText) findViewById(R.id.passphrase_edit_repeat);
this.okButton = (Button) findViewById(R.id.ok_button);
this.cancelButton = (Button) findViewById(R.id.cancel_button);
2011-12-20 18:20:44 +00:00
this.okButton.setOnClickListener(new OkButtonClickListener());
this.cancelButton.setOnClickListener(new CancelButtonClickListener());
}
2011-12-20 18:20:44 +00:00
private void verifyAndSavePassphrases() {
String passphrase = this.passphraseEdit.getText().toString();
String passphraseRepeat = this.passphraseRepeatEdit.getText().toString();
2011-12-20 18:20:44 +00:00
if (!passphrase.equals(passphraseRepeat)) {
Toast.makeText(getApplicationContext(),
R.string.PassphraseCreateActivity_passphrases_dont_match_exclamation,
Toast.LENGTH_SHORT).show();
2011-12-20 18:20:44 +00:00
this.passphraseEdit.setText("");
this.passphraseRepeatEdit.setText("");
} else {
MasterSecret masterSecret = MasterSecretUtil.generateMasterSecret(this, passphrase);
MemoryCleaner.clean(passphrase); // We do this, but the edit boxes are basically impossible to clean up.
MemoryCleaner.clean(passphraseRepeat);
new AsymmetricSecretGenerator(masterSecret).generate();
}
}
2011-12-20 18:20:44 +00:00
private class AsymmetricSecretGenerator extends Handler implements Runnable {
private ProgressDialog progressDialog;
private MasterSecret masterSecret;
2011-12-20 18:20:44 +00:00
public AsymmetricSecretGenerator(MasterSecret masterSecret) {
this.masterSecret = masterSecret;
}
2011-12-20 18:20:44 +00:00
public void run() {
MasterSecretUtil.generateAsymmetricMasterSecret(PassphraseCreateActivity.this, masterSecret);
IdentityKeyUtil.generateIdentityKeys(PassphraseCreateActivity.this, masterSecret);
this.obtainMessage().sendToTarget();
}
2011-12-20 18:20:44 +00:00
public void generate() {
progressDialog = new ProgressDialog(PassphraseCreateActivity.this);
progressDialog.setTitle(R.string.PassphraseCreateActivity_generating_keypair);
progressDialog.setMessage(getString(R.string.PassphraseCreateActivity_generating_a_local_encryption_keypair));
2011-12-20 18:20:44 +00:00
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
new Thread(this).start();
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
@Override
public void handleMessage(Message message) {
progressDialog.dismiss();
setMasterSecret(masterSecret);
}
}
2011-12-20 18:20:44 +00:00
private class CancelButtonClickListener implements OnClickListener {
public void onClick(View v) {
2011-12-20 18:20:44 +00:00
finish();
}
}
2011-12-20 18:20:44 +00:00
private class OkButtonClickListener implements OnClickListener {
public void onClick(View v) {
verifyAndSavePassphrases();
}
}
@Override
protected void cleanup() {
this.passphraseEdit = null;
this.passphraseRepeatEdit = null;
System.gc();
}
}