2012-08-08 02:09:16 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-08-08 02:09:16 +00:00
|
|
|
*
|
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.
|
2012-08-08 02:09:16 +00:00
|
|
|
*
|
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;
|
|
|
|
|
2016-02-22 20:49:51 +00:00
|
|
|
import android.os.AsyncTask;
|
2016-03-10 17:55:19 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.os.Bundle;
|
2012-08-08 02:09:16 +00:00
|
|
|
import android.text.Editable;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.EditText;
|
2016-03-10 17:55:19 +00:00
|
|
|
import android.widget.Toast;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-08-08 02:09:16 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.InvalidPassphraseException;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2012-08-08 02:09:16 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
2015-06-08 23:34:24 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
2013-07-10 01:26:18 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* Activity for changing a user's local encryption passphrase.
|
2012-08-08 02:09:16 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class PassphraseChangeActivity extends PassphraseActivity {
|
2013-07-01 17:15:36 +00:00
|
|
|
|
2015-06-08 23:34:24 +00:00
|
|
|
private DynamicTheme dynamicTheme = new DynamicTheme();
|
|
|
|
private DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
2013-07-01 17:15:36 +00:00
|
|
|
private EditText originalPassphrase;
|
|
|
|
private EditText newPassphrase;
|
|
|
|
private EditText repeatPassphrase;
|
|
|
|
private Button okButton;
|
|
|
|
private Button cancelButton;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2015-06-08 23:34:24 +00:00
|
|
|
dynamicTheme.onCreate(this);
|
|
|
|
dynamicLanguage.onCreate(this);
|
2011-12-20 18:20:44 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
setContentView(R.layout.change_passphrase_activity);
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
initializeResources();
|
|
|
|
}
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2015-06-08 23:34:24 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void initializeResources() {
|
2013-07-01 17:15:36 +00:00
|
|
|
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 18:20:44 +00:00
|
|
|
|
2013-07-01 17:15:36 +00:00
|
|
|
this.okButton = (Button ) findViewById(R.id.ok_button );
|
|
|
|
this.cancelButton = (Button ) findViewById(R.id.cancel_button );
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
this.okButton.setOnClickListener(new OkButtonClickListener());
|
|
|
|
this.cancelButton.setOnClickListener(new CancelButtonClickListener());
|
2013-07-01 17:15:36 +00:00
|
|
|
|
2013-07-10 01:26:18 +00:00
|
|
|
if (TextSecurePreferences.isPasswordDisabled(this)) {
|
2013-07-01 17:15:36 +00:00
|
|
|
this.originalPassphrase.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
this.originalPassphrase.setVisibility(View.VISIBLE);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void verifyAndSavePassphrases() {
|
2012-08-08 02:09:16 +00:00
|
|
|
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());
|
|
|
|
|
2013-07-10 01:26:18 +00:00
|
|
|
if (TextSecurePreferences.isPasswordDisabled(this)) {
|
2013-07-01 17:15:36 +00:00
|
|
|
original = MasterSecretUtil.UNENCRYPTED_PASSPHRASE;
|
|
|
|
}
|
|
|
|
|
2015-04-04 12:43:17 +00:00
|
|
|
if (!passphrase.equals(passphraseRepeat)) {
|
2016-03-10 17:55:19 +00:00
|
|
|
Toast.makeText(getApplicationContext(),
|
|
|
|
R.string.PassphraseChangeActivity_passphrases_dont_match_exclamation,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
2015-04-04 12:43:17 +00:00
|
|
|
this.newPassphrase.setText("");
|
|
|
|
this.repeatPassphrase.setText("");
|
|
|
|
} else if (passphrase.equals("")) {
|
2016-03-10 17:55:19 +00:00
|
|
|
Toast.makeText(getApplicationContext(),
|
|
|
|
R.string.PassphraseChangeActivity_enter_new_passphrase_exclamation,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
2015-04-04 12:43:17 +00:00
|
|
|
} else {
|
|
|
|
new ChangePassphraseTask(this).execute(original, passphrase);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class CancelButtonClickListener implements OnClickListener {
|
2012-08-08 02:09:16 +00:00
|
|
|
public void onClick(View v) {
|
2011-12-20 18:20:44 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class OkButtonClickListener implements OnClickListener {
|
|
|
|
public void onClick(View v) {
|
|
|
|
verifyAndSavePassphrases();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 12:43:17 +00:00
|
|
|
private class ChangePassphraseTask extends AsyncTask<String, Void, MasterSecret> {
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public ChangePassphraseTask(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute() {
|
|
|
|
okButton.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected MasterSecret doInBackground(String... params) {
|
|
|
|
try {
|
|
|
|
MasterSecret masterSecret = MasterSecretUtil.changeMasterSecretPassphrase(context, params[0], params[1]);
|
|
|
|
TextSecurePreferences.setPasswordDisabled(context, false);
|
|
|
|
|
|
|
|
return masterSecret;
|
|
|
|
|
|
|
|
} catch (InvalidPassphraseException e) {
|
|
|
|
Log.w(PassphraseChangeActivity.class.getSimpleName(), e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(MasterSecret masterSecret) {
|
|
|
|
okButton.setEnabled(true);
|
|
|
|
|
|
|
|
if (masterSecret != null) {
|
|
|
|
setMasterSecret(masterSecret);
|
|
|
|
} else {
|
2016-03-10 17:55:19 +00:00
|
|
|
Toast.makeText(context, R.string.PassphraseChangeActivity_incorrect_old_passphrase_exclamation,
|
|
|
|
Toast.LENGTH_LONG).show();
|
2015-04-04 12:43:17 +00:00
|
|
|
originalPassphrase.setText("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
protected void cleanup() {
|
|
|
|
this.originalPassphrase = null;
|
|
|
|
this.newPassphrase = null;
|
|
|
|
this.repeatPassphrase = null;
|
2012-08-08 02:09:16 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
System.gc();
|
|
|
|
}
|
|
|
|
}
|