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

146 lines
5.1 KiB
Java
Raw Normal View History

/**
2011-12-20 19:20:44 +01:00
* Copyright (C) 2011 Whisper Systems
*
2011-12-20 19:20:44 +01: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 19:20:44 +01: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.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
2011-12-20 19:20:44 +01:00
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
2011-12-20 19:20:44 +01:00
import android.widget.Toast;
import org.thoughtcrime.securesms.crypto.InvalidPassphraseException;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.util.MemoryCleaner;
2011-12-20 19:20:44 +01:00
/**
* Activity for changing a user's local encryption passphrase.
*
2011-12-20 19:20:44 +01:00
* @author Moxie Marlinspike
*/
public class PassphraseChangeActivity extends PassphraseActivity {
private EditText originalPassphrase;
private EditText newPassphrase;
private EditText repeatPassphrase;
private TextView originalPassphraseLabel;
private Button okButton;
private Button cancelButton;
2011-12-20 19:20:44 +01:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2011-12-20 19:20:44 +01:00
setContentView(R.layout.change_passphrase_activity);
2011-12-20 19:20:44 +01:00
initializeResources();
}
2011-12-20 19:20:44 +01:00
private void initializeResources() {
this.originalPassphraseLabel = (TextView) findViewById(R.id.old_passphrase_label);
this.originalPassphrase = (EditText) findViewById(R.id.old_passphrase );
this.newPassphrase = (EditText) findViewById(R.id.new_passphrase );
this.repeatPassphrase = (EditText) findViewById(R.id.repeat_passphrase );
2011-12-20 19:20:44 +01:00
this.okButton = (Button ) findViewById(R.id.ok_button );
this.cancelButton = (Button ) findViewById(R.id.cancel_button );
2011-12-20 19:20:44 +01:00
this.okButton.setOnClickListener(new OkButtonClickListener());
this.cancelButton.setOnClickListener(new CancelButtonClickListener());
if (isPassphraseDisabled()) {
this.originalPassphrase.setVisibility(View.GONE);
this.originalPassphraseLabel.setVisibility(View.GONE);
} else {
this.originalPassphrase.setVisibility(View.VISIBLE);
this.originalPassphraseLabel.setVisibility(View.VISIBLE);
}
2011-12-20 19:20:44 +01:00
}
2011-12-20 19:20:44 +01:00
private void verifyAndSavePassphrases() {
Editable originalText = this.originalPassphrase.getText();
Editable newText = this.newPassphrase.getText();
Editable repeatText = this.repeatPassphrase.getText();
String original = (originalText == null ? "" : originalText.toString());
String passphrase = (newText == null ? "" : newText.toString());
String passphraseRepeat = (repeatText == null ? "" : repeatText.toString());
if (isPassphraseDisabled()) {
original = MasterSecretUtil.UNENCRYPTED_PASSPHRASE;
}
2011-12-20 19:20:44 +01:00
try {
if (!passphrase.equals(passphraseRepeat)) {
Toast.makeText(getApplicationContext(),
R.string.PassphraseChangeActivity_passphrases_dont_match_exclamation,
Toast.LENGTH_SHORT).show();
2011-12-20 19:20:44 +01:00
this.newPassphrase.setText("");
this.repeatPassphrase.setText("");
} else {
MasterSecret masterSecret = MasterSecretUtil.changeMasterSecretPassphrase(this, original, passphrase);
PreferenceManager.getDefaultSharedPreferences(this)
.edit()
.putBoolean(ApplicationPreferencesActivity.DISABLE_PASSPHRASE_PREF, false)
.commit();
2011-12-20 19:20:44 +01:00
MemoryCleaner.clean(original);
MemoryCleaner.clean(passphrase);
MemoryCleaner.clean(passphraseRepeat);
2011-12-20 19:20:44 +01:00
setMasterSecret(masterSecret);
}
} catch (InvalidPassphraseException e) {
Toast.makeText(this, R.string.PassphraseChangeActivity_incorrect_old_passphrase_exclamation,
Toast.LENGTH_LONG).show();
2011-12-20 19:20:44 +01:00
this.originalPassphrase.setText("");
}
}
private boolean isPassphraseDisabled() {
return PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(ApplicationPreferencesActivity.DISABLE_PASSPHRASE_PREF, false);
}
2011-12-20 19:20:44 +01:00
private class CancelButtonClickListener implements OnClickListener {
public void onClick(View v) {
2011-12-20 19:20:44 +01:00
finish();
}
}
2011-12-20 19:20:44 +01:00
private class OkButtonClickListener implements OnClickListener {
public void onClick(View v) {
verifyAndSavePassphrases();
}
}
@Override
protected void cleanup() {
this.originalPassphrase = null;
this.newPassphrase = null;
this.repeatPassphrase = null;
2011-12-20 19:20:44 +01:00
System.gc();
}
}