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

206 lines
6.9 KiB
Java
Raw Normal View History

2012-07-31 14:18:14 -07:00
/**
2011-12-20 10:20:44 -08:00
* Copyright (C) 2011 Whisper Systems
2012-07-31 14:18:14 -07:00
*
2011-12-20 10:20:44 -08: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-07-31 14:18:14 -07:00
*
2011-12-20 10:20:44 -08: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;
2014-02-25 19:53:12 -08:00
import android.content.Intent;
2011-12-20 10:20:44 -08:00
import android.os.Bundle;
2014-06-28 20:40:57 -07:00
import android.support.v7.app.ActionBar;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.TypefaceSpan;
import android.view.KeyEvent;
2014-06-28 20:40:57 -07:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2011-12-20 10:20:44 -08:00
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
2011-12-20 10:20:44 -08:00
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
2011-12-20 10:20:44 -08:00
import org.thoughtcrime.securesms.components.AnimatingToggle;
2012-07-31 14:18:14 -07:00
import org.thoughtcrime.securesms.crypto.InvalidPassphraseException;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.util.DynamicIntroTheme;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.crypto.MasterSecret;
2012-07-31 14:18:14 -07:00
2011-12-20 10:20:44 -08:00
/**
* Activity that prompts for a user's passphrase.
2012-07-31 14:18:14 -07:00
*
* @author Moxie Marlinspike
2011-12-20 10:20:44 -08:00
*/
public class PassphrasePromptActivity extends PassphraseActivity {
private DynamicIntroTheme dynamicTheme = new DynamicIntroTheme();
private DynamicLanguage dynamicLanguage = new DynamicLanguage();
private EditText passphraseText;
private ImageButton showButton;
private ImageButton hideButton;
private AnimatingToggle visibilityToggle;
2012-07-31 14:18:14 -07:00
2011-12-20 10:20:44 -08:00
@Override
public void onCreate(Bundle savedInstanceState) {
dynamicTheme.onCreate(this);
dynamicLanguage.onCreate(this);
2011-12-20 10:20:44 -08:00
super.onCreate(savedInstanceState);
setContentView(R.layout.prompt_passphrase_activity);
initializeResources();
}
2012-07-31 14:18:14 -07:00
@Override
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
2014-02-25 19:53:12 -08:00
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
2014-06-28 20:40:57 -07:00
MenuInflater inflater = this.getMenuInflater();
2014-02-25 19:53:12 -08:00
menu.clear();
inflater.inflate(R.menu.log_submit, menu);
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.menu_submit_debug_logs: handleLogSubmit(); return true;
}
return false;
}
private void handleLogSubmit() {
Intent intent = new Intent(this, LogSubmitActivity.class);
startActivity(intent);
}
private void handlePassphrase() {
try {
Editable text = passphraseText.getText();
String passphrase = (text == null ? "" : text.toString());
MasterSecret masterSecret = MasterSecretUtil.getMasterSecret(this, passphrase);
setMasterSecret(masterSecret);
} catch (InvalidPassphraseException ipe) {
passphraseText.setText("");
passphraseText.setError(
getString(R.string.PassphrasePromptActivity_invalid_passphrase_exclamation));
}
}
private void setPassphraseVisibility(boolean visibility) {
int cursorPosition = passphraseText.getSelectionStart();
if (visibility) {
passphraseText.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
passphraseText.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
passphraseText.setSelection(cursorPosition);
}
2011-12-20 10:20:44 -08:00
private void initializeResources() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.centered_app_title);
ImageButton okButton = (ImageButton) findViewById(R.id.ok_button);
showButton = (ImageButton) findViewById(R.id.passphrase_visibility);
hideButton = (ImageButton) findViewById(R.id.passphrase_visibility_off);
visibilityToggle = (AnimatingToggle) findViewById(R.id.button_toggle);
passphraseText = (EditText) findViewById(R.id.passphrase_edit);
2015-01-20 21:26:58 -10:00
SpannableString hint = new SpannableString(" " + getString(R.string.PassphrasePromptActivity_enter_passphrase));
hint.setSpan(new RelativeSizeSpan(0.9f), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
hint.setSpan(new TypefaceSpan("sans-serif"), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
2015-01-20 21:26:58 -10:00
passphraseText.setHint(hint);
2011-12-20 10:20:44 -08:00
okButton.setOnClickListener(new OkButtonClickListener());
showButton.setOnClickListener(new ShowButtonOnClickListener());
hideButton.setOnClickListener(new HideButtonOnClickListener());
passphraseText.setOnEditorActionListener(new PassphraseActionListener());
passphraseText.setImeActionLabel(getString(R.string.prompt_passphrase_activity__unlock),
EditorInfo.IME_ACTION_DONE);
}
private class PassphraseActionListener implements TextView.OnEditorActionListener {
@Override
public boolean onEditorAction(TextView exampleView, int actionId, KeyEvent keyEvent) {
if ((keyEvent == null && actionId == EditorInfo.IME_ACTION_DONE) ||
(keyEvent != null && keyEvent.getAction() == KeyEvent.ACTION_DOWN &&
(actionId == EditorInfo.IME_NULL)))
{
handlePassphrase();
return true;
} else if (keyEvent != null && keyEvent.getAction() == KeyEvent.ACTION_UP &&
actionId == EditorInfo.IME_NULL)
{
return true;
}
return false;
}
2011-12-20 10:20:44 -08:00
}
2012-07-31 14:18:14 -07:00
2011-12-20 10:20:44 -08:00
private class OkButtonClickListener implements OnClickListener {
@Override
2011-12-20 10:20:44 -08:00
public void onClick(View v) {
handlePassphrase();
2011-12-20 10:20:44 -08:00
}
}
2012-07-31 14:18:14 -07:00
private class ShowButtonOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
visibilityToggle.display(hideButton);
setPassphraseVisibility(true);
}
}
private class HideButtonOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
visibilityToggle.display(showButton);
setPassphraseVisibility(false);
}
}
2011-12-20 10:20:44 -08:00
@Override
protected void cleanup() {
2013-05-06 19:27:09 -07:00
this.passphraseText.setText("");
2011-12-20 10:20:44 -08:00
System.gc();
}
}