2010-05-09 15:51:21 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.thialfihar.android.apg;
|
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.security.SignatureException;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
|
|
import org.bouncycastle2.openpgp.PGPException;
|
|
|
|
import org.bouncycastle2.openpgp.PGPPublicKey;
|
|
|
|
import org.bouncycastle2.openpgp.PGPPublicKeyRing;
|
|
|
|
import org.bouncycastle2.openpgp.PGPSecretKey;
|
|
|
|
import org.bouncycastle2.openpgp.PGPSecretKeyRing;
|
2010-05-16 10:20:17 -04:00
|
|
|
import org.thialfihar.android.apg.utils.Choice;
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.ActivityNotFoundException;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Message;
|
|
|
|
import android.text.ClipboardManager;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.animation.AnimationUtils;
|
2010-05-16 10:20:17 -04:00
|
|
|
import android.widget.ArrayAdapter;
|
2010-05-09 15:51:21 -04:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
2010-05-16 10:20:17 -04:00
|
|
|
import android.widget.Spinner;
|
2010-05-09 15:51:21 -04:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import android.widget.ViewFlipper;
|
|
|
|
|
|
|
|
public class EncryptActivity extends BaseActivity {
|
2010-05-31 23:20:13 -04:00
|
|
|
private Intent mIntent = null;
|
2010-05-09 15:51:21 -04:00
|
|
|
private String mSubject = null;
|
|
|
|
private String mSendTo = null;
|
|
|
|
|
|
|
|
private long mEncryptionKeyIds[] = null;
|
|
|
|
|
2010-06-02 06:45:26 -04:00
|
|
|
private boolean mReturnResult = false;
|
2010-05-09 15:51:21 -04:00
|
|
|
private EditText mMessage = null;
|
|
|
|
private Button mSelectKeysButton = null;
|
|
|
|
private Button mEncryptButton = null;
|
|
|
|
private Button mEncryptToClipboardButton = null;
|
|
|
|
private CheckBox mSign = null;
|
|
|
|
private TextView mMainUserId = null;
|
|
|
|
private TextView mMainUserIdRest = null;
|
|
|
|
|
|
|
|
private ViewFlipper mSource = null;
|
|
|
|
private TextView mSourceLabel = null;
|
|
|
|
private ImageView mSourcePrevious = null;
|
|
|
|
private ImageView mSourceNext = null;
|
|
|
|
|
|
|
|
private ViewFlipper mMode = null;
|
|
|
|
private TextView mModeLabel = null;
|
|
|
|
private ImageView mModePrevious = null;
|
|
|
|
private ImageView mModeNext = null;
|
|
|
|
|
|
|
|
private int mEncryptTarget;
|
|
|
|
|
|
|
|
private EditText mPassPhrase = null;
|
|
|
|
private EditText mPassPhraseAgain = null;
|
|
|
|
private CheckBox mAsciiArmour = null;
|
2010-05-16 10:20:17 -04:00
|
|
|
private Spinner mFileCompression = null;
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
private EditText mFilename = null;
|
|
|
|
private CheckBox mDeleteAfter = null;
|
|
|
|
private ImageButton mBrowse = null;
|
|
|
|
|
|
|
|
private String mInputFilename = null;
|
|
|
|
private String mOutputFilename = null;
|
|
|
|
|
2010-07-09 16:55:17 -04:00
|
|
|
private boolean mAsciiArmourDemand = false;
|
|
|
|
private boolean mOverrideAsciiArmour = false;
|
|
|
|
private Uri mContentUri = null;
|
|
|
|
private byte[] mData = null;
|
|
|
|
|
|
|
|
private DataSource mDataSource = null;
|
|
|
|
private DataDestination mDataDestination = null;
|
|
|
|
|
2010-12-25 09:00:25 -05:00
|
|
|
private boolean mGenerateSignature = false;
|
|
|
|
|
2010-05-09 15:51:21 -04:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.encrypt);
|
|
|
|
|
2010-12-25 09:00:25 -05:00
|
|
|
mGenerateSignature = false;
|
|
|
|
|
2010-05-09 15:51:21 -04:00
|
|
|
mSource = (ViewFlipper) findViewById(R.id.source);
|
2010-05-13 16:41:32 -04:00
|
|
|
mSourceLabel = (TextView) findViewById(R.id.sourceLabel);
|
|
|
|
mSourcePrevious = (ImageView) findViewById(R.id.sourcePrevious);
|
|
|
|
mSourceNext = (ImageView) findViewById(R.id.sourceNext);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
mSourcePrevious.setClickable(true);
|
|
|
|
mSourcePrevious.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
mSource.setInAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_right_in));
|
|
|
|
mSource.setOutAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_right_out));
|
|
|
|
mSource.showPrevious();
|
|
|
|
updateSource();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mSourceNext.setClickable(true);
|
|
|
|
OnClickListener nextSourceClickListener = new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
mSource.setInAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_left_in));
|
|
|
|
mSource.setOutAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_left_out));
|
|
|
|
mSource.showNext();
|
|
|
|
updateSource();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mSourceNext.setOnClickListener(nextSourceClickListener);
|
|
|
|
|
|
|
|
mSourceLabel.setClickable(true);
|
|
|
|
mSourceLabel.setOnClickListener(nextSourceClickListener);
|
|
|
|
|
|
|
|
mMode = (ViewFlipper) findViewById(R.id.mode);
|
2010-05-13 16:41:32 -04:00
|
|
|
mModeLabel = (TextView) findViewById(R.id.modeLabel);
|
|
|
|
mModePrevious = (ImageView) findViewById(R.id.modePrevious);
|
|
|
|
mModeNext = (ImageView) findViewById(R.id.modeNext);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
mModePrevious.setClickable(true);
|
|
|
|
mModePrevious.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
mMode.setInAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_right_in));
|
|
|
|
mMode.setOutAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_right_out));
|
|
|
|
mMode.showPrevious();
|
|
|
|
updateMode();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
OnClickListener nextModeClickListener = new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
mMode.setInAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_left_in));
|
|
|
|
mMode.setOutAnimation(AnimationUtils.loadAnimation(EncryptActivity.this,
|
|
|
|
R.anim.push_left_out));
|
|
|
|
mMode.showNext();
|
|
|
|
updateMode();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mModeNext.setOnClickListener(nextModeClickListener);
|
|
|
|
|
|
|
|
mModeLabel.setClickable(true);
|
|
|
|
mModeLabel.setOnClickListener(nextModeClickListener);
|
|
|
|
|
|
|
|
mMessage = (EditText) findViewById(R.id.message);
|
|
|
|
mSelectKeysButton = (Button) findViewById(R.id.btn_selectEncryptKeys);
|
|
|
|
mEncryptButton = (Button) findViewById(R.id.btn_encrypt);
|
2010-05-13 16:41:32 -04:00
|
|
|
mEncryptToClipboardButton = (Button) findViewById(R.id.btn_encryptToClipboard);
|
2010-05-09 15:51:21 -04:00
|
|
|
mSign = (CheckBox) findViewById(R.id.sign);
|
2010-05-13 16:41:32 -04:00
|
|
|
mMainUserId = (TextView) findViewById(R.id.mainUserId);
|
|
|
|
mMainUserIdRest = (TextView) findViewById(R.id.mainUserIdRest);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
2010-05-13 16:41:32 -04:00
|
|
|
mPassPhrase = (EditText) findViewById(R.id.passPhrase);
|
|
|
|
mPassPhraseAgain = (EditText) findViewById(R.id.passPhraseAgain);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
// measure the height of the source_file view and set the message view's min height to that,
|
|
|
|
// so it fills mSource fully... bit of a hack.
|
2010-05-13 16:41:32 -04:00
|
|
|
View tmp = findViewById(R.id.sourceFile);
|
2010-05-09 15:51:21 -04:00
|
|
|
tmp.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
int height = tmp.getMeasuredHeight();
|
|
|
|
mMessage.setMinimumHeight(height);
|
|
|
|
|
|
|
|
mFilename = (EditText) findViewById(R.id.filename);
|
|
|
|
mBrowse = (ImageButton) findViewById(R.id.btn_browse);
|
|
|
|
mBrowse.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
openFile();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-05-16 10:20:17 -04:00
|
|
|
mFileCompression = (Spinner) findViewById(R.id.fileCompression);
|
|
|
|
Choice[] choices = new Choice[] {
|
2010-07-05 09:22:03 -04:00
|
|
|
new Choice(Id.choice.compression.none, getString(R.string.choice_none) +
|
|
|
|
" (" + getString(R.string.fast) + ")"),
|
|
|
|
new Choice(Id.choice.compression.zip, "ZIP (" + getString(R.string.fast) + ")"),
|
|
|
|
new Choice(Id.choice.compression.zlib, "ZLIB (" + getString(R.string.fast) + ")"),
|
|
|
|
new Choice(Id.choice.compression.bzip2, "BZIP2 (" + getString(R.string.very_slow) + ")"),
|
2010-05-16 10:20:17 -04:00
|
|
|
};
|
|
|
|
ArrayAdapter<Choice> adapter =
|
|
|
|
new ArrayAdapter<Choice>(this, android.R.layout.simple_spinner_item, choices);
|
|
|
|
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
|
mFileCompression.setAdapter(adapter);
|
|
|
|
|
2010-06-17 09:23:07 -04:00
|
|
|
int defaultFileCompression = mPreferences.getDefaultFileCompression();
|
2010-05-16 10:20:17 -04:00
|
|
|
for (int i = 0; i < choices.length; ++i) {
|
|
|
|
if (choices[i].getId() == defaultFileCompression) {
|
|
|
|
mFileCompression.setSelection(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-13 16:41:32 -04:00
|
|
|
mDeleteAfter = (CheckBox) findViewById(R.id.deleteAfterEncryption);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
2010-05-13 16:41:32 -04:00
|
|
|
mAsciiArmour = (CheckBox) findViewById(R.id.asciiArmour);
|
2010-06-17 09:23:07 -04:00
|
|
|
mAsciiArmour.setChecked(mPreferences.getDefaultAsciiArmour());
|
2010-05-09 15:51:21 -04:00
|
|
|
mAsciiArmour.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
guessOutputFilename();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mEncryptToClipboardButton.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
encryptToClipboardClicked();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mEncryptButton.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
encryptClicked();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mSelectKeysButton.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
selectPublicKeys();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mSign.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
CheckBox checkBox = (CheckBox) v;
|
|
|
|
if (checkBox.isChecked()) {
|
|
|
|
selectSecretKey();
|
|
|
|
} else {
|
2010-05-15 11:19:56 -04:00
|
|
|
setSecretKeyId(Id.key.none);
|
2010-05-09 15:51:21 -04:00
|
|
|
updateView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-05-31 23:20:13 -04:00
|
|
|
mIntent = getIntent();
|
2010-06-02 06:45:26 -04:00
|
|
|
if (Apg.Intent.ENCRYPT.equals(mIntent.getAction()) ||
|
|
|
|
Apg.Intent.ENCRYPT_FILE.equals(mIntent.getAction()) ||
|
2010-12-25 09:00:25 -05:00
|
|
|
Apg.Intent.ENCRYPT_AND_RETURN.equals(mIntent.getAction()) ||
|
|
|
|
Apg.Intent.GENERATE_SIGNATURE.equals(mIntent.getAction())) {
|
2010-07-09 16:55:17 -04:00
|
|
|
mContentUri = mIntent.getData();
|
2010-05-31 23:20:13 -04:00
|
|
|
Bundle extras = mIntent.getExtras();
|
2010-05-09 15:51:21 -04:00
|
|
|
if (extras == null) {
|
|
|
|
extras = new Bundle();
|
|
|
|
}
|
2010-06-02 06:45:26 -04:00
|
|
|
|
2010-12-25 09:00:25 -05:00
|
|
|
if (Apg.Intent.ENCRYPT_AND_RETURN.equals(mIntent.getAction()) ||
|
|
|
|
Apg.Intent.GENERATE_SIGNATURE.equals(mIntent.getAction())) {
|
2010-06-02 06:45:26 -04:00
|
|
|
mReturnResult = true;
|
|
|
|
}
|
|
|
|
|
2010-12-25 09:00:25 -05:00
|
|
|
if (Apg.Intent.GENERATE_SIGNATURE.equals(mIntent.getAction())) {
|
|
|
|
mGenerateSignature = true;
|
|
|
|
mOverrideAsciiArmour = true;
|
|
|
|
mAsciiArmourDemand = false;
|
|
|
|
}
|
|
|
|
|
2010-07-09 16:55:17 -04:00
|
|
|
if (extras.containsKey(Apg.EXTRA_ASCII_ARMOUR)) {
|
|
|
|
mAsciiArmourDemand = extras.getBoolean(Apg.EXTRA_ASCII_ARMOUR, true);
|
|
|
|
mOverrideAsciiArmour = true;
|
|
|
|
mAsciiArmour.setChecked(mAsciiArmourDemand);
|
|
|
|
}
|
|
|
|
|
|
|
|
mData = extras.getByteArray(Apg.EXTRA_DATA);
|
|
|
|
String textData = null;
|
|
|
|
if (mData == null) {
|
|
|
|
textData = extras.getString(Apg.EXTRA_TEXT);
|
|
|
|
}
|
2010-05-31 19:15:20 -04:00
|
|
|
mSendTo = extras.getString(Apg.EXTRA_SEND_TO);
|
|
|
|
mSubject = extras.getString(Apg.EXTRA_SUBJECT);
|
|
|
|
long signatureKeyId = extras.getLong(Apg.EXTRA_SIGNATURE_KEY_ID);
|
|
|
|
long encryptionKeyIds[] = extras.getLongArray(Apg.EXTRA_ENCRYPTION_KEY_IDS);
|
2010-05-09 15:51:21 -04:00
|
|
|
if (signatureKeyId != 0) {
|
2010-05-25 20:33:26 -04:00
|
|
|
PGPSecretKeyRing keyRing = Apg.getSecretKeyRing(signatureKeyId);
|
2010-05-09 15:51:21 -04:00
|
|
|
PGPSecretKey masterKey = null;
|
|
|
|
if (keyRing != null) {
|
|
|
|
masterKey = Apg.getMasterKey(keyRing);
|
|
|
|
if (masterKey != null) {
|
|
|
|
Vector<PGPSecretKey> signKeys = Apg.getUsableSigningKeys(keyRing);
|
|
|
|
if (signKeys.size() > 0) {
|
|
|
|
setSecretKeyId(masterKey.getKeyID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encryptionKeyIds != null) {
|
|
|
|
Vector<Long> goodIds = new Vector<Long>();
|
|
|
|
for (int i = 0; i < encryptionKeyIds.length; ++i) {
|
2010-05-25 20:33:26 -04:00
|
|
|
PGPPublicKeyRing keyRing = Apg.getPublicKeyRing(encryptionKeyIds[i]);
|
2010-05-09 15:51:21 -04:00
|
|
|
PGPPublicKey masterKey = null;
|
|
|
|
if (keyRing == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
masterKey = Apg.getMasterKey(keyRing);
|
|
|
|
if (masterKey == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Vector<PGPPublicKey> encryptKeys = Apg.getUsableEncryptKeys(keyRing);
|
|
|
|
if (encryptKeys.size() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goodIds.add(masterKey.getKeyID());
|
|
|
|
}
|
|
|
|
if (goodIds.size() > 0) {
|
|
|
|
mEncryptionKeyIds = new long[goodIds.size()];
|
|
|
|
for (int i = 0; i < goodIds.size(); ++i) {
|
|
|
|
mEncryptionKeyIds[i] = goodIds.get(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-02 06:45:26 -04:00
|
|
|
if (Apg.Intent.ENCRYPT.equals(mIntent.getAction()) ||
|
2010-12-25 09:00:25 -05:00
|
|
|
Apg.Intent.ENCRYPT_AND_RETURN.equals(mIntent.getAction()) ||
|
|
|
|
Apg.Intent.GENERATE_SIGNATURE.equals(mIntent.getAction())) {
|
2010-07-09 16:55:17 -04:00
|
|
|
if (textData != null) {
|
|
|
|
mMessage.setText(textData);
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
mSource.setInAnimation(null);
|
|
|
|
mSource.setOutAnimation(null);
|
2010-05-13 16:41:32 -04:00
|
|
|
while (mSource.getCurrentView().getId() != R.id.sourceMessage) {
|
2010-05-09 15:51:21 -04:00
|
|
|
mSource.showNext();
|
|
|
|
}
|
2010-06-02 06:45:26 -04:00
|
|
|
} else if (Apg.Intent.ENCRYPT_FILE.equals(mIntent.getAction())) {
|
2010-06-05 20:35:00 -04:00
|
|
|
if ("file".equals(mIntent.getScheme())) {
|
2010-08-17 08:50:42 -04:00
|
|
|
mInputFilename = Uri.decode(mIntent.getDataString().replace("file://", ""));
|
2010-06-05 20:35:00 -04:00
|
|
|
mFilename.setText(mInputFilename);
|
|
|
|
guessOutputFilename();
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
mSource.setInAnimation(null);
|
|
|
|
mSource.setOutAnimation(null);
|
2010-05-13 16:41:32 -04:00
|
|
|
while (mSource.getCurrentView().getId() != R.id.sourceFile) {
|
2010-05-09 15:51:21 -04:00
|
|
|
mSource.showNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateView();
|
|
|
|
updateSource();
|
|
|
|
updateMode();
|
2010-05-31 23:20:13 -04:00
|
|
|
|
2010-06-02 11:33:40 -04:00
|
|
|
if (mReturnResult) {
|
|
|
|
mSourcePrevious.setClickable(false);
|
|
|
|
mSourcePrevious.setEnabled(false);
|
|
|
|
mSourcePrevious.setVisibility(View.INVISIBLE);
|
|
|
|
|
|
|
|
mSourceNext.setClickable(false);
|
|
|
|
mSourceNext.setEnabled(false);
|
|
|
|
mSourceNext.setVisibility(View.INVISIBLE);
|
|
|
|
|
|
|
|
mSourceLabel.setClickable(false);
|
|
|
|
mSourceLabel.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2010-07-24 14:54:40 -04:00
|
|
|
updateButtons();
|
|
|
|
|
2010-06-02 15:30:27 -04:00
|
|
|
if (mReturnResult &&
|
2010-09-26 13:46:13 -04:00
|
|
|
(mMessage.getText().length() > 0 || mData != null) &&
|
2010-05-31 23:20:13 -04:00
|
|
|
((mEncryptionKeyIds != null &&
|
|
|
|
mEncryptionKeyIds.length > 0) ||
|
2010-06-01 10:59:06 -04:00
|
|
|
getSecretKeyId() != 0)) {
|
2010-05-31 23:20:13 -04:00
|
|
|
encryptClicked();
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void openFile() {
|
|
|
|
String filename = mFilename.getText().toString();
|
|
|
|
|
2010-06-01 17:28:30 -04:00
|
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
intent.setData(Uri.parse("file://" + filename));
|
2010-06-01 17:28:30 -04:00
|
|
|
intent.setType("*/*");
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
startActivityForResult(intent, Id.request.filename);
|
|
|
|
} catch (ActivityNotFoundException e) {
|
|
|
|
// No compatible file manager was found.
|
2010-06-01 17:28:30 -04:00
|
|
|
Toast.makeText(this, R.string.noFilemanagerInstalled, Toast.LENGTH_SHORT).show();
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void guessOutputFilename() {
|
|
|
|
mInputFilename = mFilename.getText().toString();
|
|
|
|
File file = new File(mInputFilename);
|
|
|
|
String ending = (mAsciiArmour.isChecked() ? ".asc" : ".gpg");
|
|
|
|
mOutputFilename = Constants.path.app_dir + "/" + file.getName() + ending;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSource() {
|
|
|
|
switch (mSource.getCurrentView().getId()) {
|
2010-05-13 16:41:32 -04:00
|
|
|
case R.id.sourceFile: {
|
2010-05-09 15:51:21 -04:00
|
|
|
mSourceLabel.setText(R.string.label_file);
|
2010-07-24 14:54:40 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case R.id.sourceMessage: {
|
|
|
|
mSourceLabel.setText(R.string.label_message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateButtons() {
|
|
|
|
switch (mSource.getCurrentView().getId()) {
|
|
|
|
case R.id.sourceFile: {
|
2010-06-05 20:35:00 -04:00
|
|
|
mEncryptToClipboardButton.setVisibility(View.INVISIBLE);
|
2010-07-24 14:54:40 -04:00
|
|
|
mEncryptButton.setText(R.string.btn_encrypt);
|
2010-05-09 15:51:21 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-13 16:41:32 -04:00
|
|
|
case R.id.sourceMessage: {
|
2010-05-09 15:51:21 -04:00
|
|
|
mSourceLabel.setText(R.string.label_message);
|
2010-07-24 14:54:40 -04:00
|
|
|
if (mReturnResult) {
|
|
|
|
mEncryptToClipboardButton.setVisibility(View.INVISIBLE);
|
|
|
|
} else {
|
|
|
|
mEncryptToClipboardButton.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
if (mMode.getCurrentView().getId() == R.id.modeSymmetric) {
|
|
|
|
if (mReturnResult) {
|
|
|
|
mEncryptButton.setText(R.string.btn_encrypt);
|
|
|
|
} else {
|
|
|
|
mEncryptButton.setText(R.string.btn_encryptAndEmail);
|
|
|
|
}
|
|
|
|
mEncryptButton.setEnabled(true);
|
|
|
|
mEncryptToClipboardButton.setText(R.string.btn_encryptToClipboard);
|
|
|
|
mEncryptToClipboardButton.setEnabled(true);
|
|
|
|
} else {
|
|
|
|
if (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0) {
|
|
|
|
if (getSecretKeyId() == 0) {
|
|
|
|
if (mReturnResult) {
|
|
|
|
mEncryptButton.setText(R.string.btn_encrypt);
|
|
|
|
} else {
|
|
|
|
mEncryptButton.setText(R.string.btn_encryptAndEmail);
|
|
|
|
}
|
|
|
|
mEncryptButton.setEnabled(false);
|
|
|
|
mEncryptToClipboardButton.setText(R.string.btn_encryptToClipboard);
|
|
|
|
mEncryptToClipboardButton.setEnabled(false);
|
|
|
|
} else {
|
|
|
|
if (mReturnResult) {
|
|
|
|
mEncryptButton.setText(R.string.btn_sign);
|
|
|
|
} else {
|
|
|
|
mEncryptButton.setText(R.string.btn_signAndEmail);
|
|
|
|
}
|
|
|
|
mEncryptButton.setEnabled(true);
|
|
|
|
mEncryptToClipboardButton.setText(R.string.btn_signToClipboard);
|
|
|
|
mEncryptToClipboardButton.setEnabled(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (mReturnResult) {
|
|
|
|
mEncryptButton.setText(R.string.btn_encrypt);
|
|
|
|
} else {
|
|
|
|
mEncryptButton.setText(R.string.btn_encryptAndEmail);
|
|
|
|
}
|
|
|
|
mEncryptButton.setEnabled(true);
|
|
|
|
mEncryptToClipboardButton.setText(R.string.btn_encryptToClipboard);
|
|
|
|
mEncryptToClipboardButton.setEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateMode() {
|
|
|
|
switch (mMode.getCurrentView().getId()) {
|
2010-05-13 16:41:32 -04:00
|
|
|
case R.id.modeAsymmetric: {
|
2010-05-09 15:51:21 -04:00
|
|
|
mModeLabel.setText(R.string.label_asymmetric);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-13 16:41:32 -04:00
|
|
|
case R.id.modeSymmetric: {
|
2010-05-09 15:51:21 -04:00
|
|
|
mModeLabel.setText(R.string.label_symmetric);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-24 14:54:40 -04:00
|
|
|
updateButtons();
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void encryptToClipboardClicked() {
|
|
|
|
mEncryptTarget = Id.target.clipboard;
|
|
|
|
initiateEncryption();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void encryptClicked() {
|
2010-05-13 16:41:32 -04:00
|
|
|
if (mSource.getCurrentView().getId() == R.id.sourceFile) {
|
2010-05-09 15:51:21 -04:00
|
|
|
mEncryptTarget = Id.target.file;
|
|
|
|
} else {
|
|
|
|
mEncryptTarget = Id.target.email;
|
|
|
|
}
|
|
|
|
initiateEncryption();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initiateEncryption() {
|
|
|
|
if (mEncryptTarget == Id.target.file) {
|
|
|
|
String currentFilename = mFilename.getText().toString();
|
|
|
|
if (mInputFilename == null || !mInputFilename.equals(currentFilename)) {
|
|
|
|
guessOutputFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInputFilename.equals("")) {
|
2010-05-13 16:41:32 -04:00
|
|
|
Toast.makeText(this, R.string.noFileSelected, Toast.LENGTH_SHORT).show();
|
2010-05-09 15:51:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-05 09:22:03 -04:00
|
|
|
if (!mInputFilename.startsWith("content")) {
|
|
|
|
File file = new File(mInputFilename);
|
|
|
|
if (!file.exists() || !file.isFile()) {
|
|
|
|
Toast.makeText(this, getString(R.string.errorMessage,
|
|
|
|
getString(R.string.error_fileNotFound)),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// symmetric encryption
|
2010-05-13 16:41:32 -04:00
|
|
|
if (mMode.getCurrentView().getId() == R.id.modeSymmetric) {
|
2010-05-09 15:51:21 -04:00
|
|
|
boolean gotPassPhrase = false;
|
|
|
|
String passPhrase = mPassPhrase.getText().toString();
|
|
|
|
String passPhraseAgain = mPassPhraseAgain.getText().toString();
|
|
|
|
if (!passPhrase.equals(passPhraseAgain)) {
|
2010-05-13 16:41:32 -04:00
|
|
|
Toast.makeText(this, R.string.passPhrasesDoNotMatch, Toast.LENGTH_SHORT).show();
|
2010-05-09 15:51:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gotPassPhrase = (passPhrase.length() != 0);
|
|
|
|
if (!gotPassPhrase) {
|
2010-05-13 16:41:32 -04:00
|
|
|
Toast.makeText(this, R.string.passPhraseMustNotBeEmpty, Toast.LENGTH_SHORT).show();
|
2010-05-09 15:51:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2010-06-01 10:59:06 -04:00
|
|
|
boolean encryptIt = (mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0);
|
2010-05-09 15:51:21 -04:00
|
|
|
// for now require at least one form of encryption for files
|
|
|
|
if (!encryptIt && mEncryptTarget == Id.target.file) {
|
2010-05-13 16:41:32 -04:00
|
|
|
Toast.makeText(this, R.string.selectEncryptionKey, Toast.LENGTH_SHORT).show();
|
2010-05-09 15:51:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!encryptIt && getSecretKeyId() == 0) {
|
2010-05-13 16:41:32 -04:00
|
|
|
Toast.makeText(this, R.string.selectEncryptionOrSignatureKey,
|
2010-05-09 15:51:21 -04:00
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-15 11:19:56 -04:00
|
|
|
if (getSecretKeyId() != 0 && Apg.getCachedPassPhrase(getSecretKeyId()) == null) {
|
2010-05-09 15:51:21 -04:00
|
|
|
showDialog(Id.dialog.pass_phrase);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEncryptTarget == Id.target.file) {
|
|
|
|
askForOutputFilename();
|
|
|
|
} else {
|
|
|
|
encryptStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void askForOutputFilename() {
|
|
|
|
showDialog(Id.dialog.output_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2010-05-15 11:19:56 -04:00
|
|
|
public void passPhraseCallback(long keyId, String passPhrase) {
|
|
|
|
super.passPhraseCallback(keyId, passPhrase);
|
2010-05-09 15:51:21 -04:00
|
|
|
if (mEncryptTarget == Id.target.file) {
|
|
|
|
askForOutputFilename();
|
|
|
|
} else {
|
|
|
|
encryptStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void encryptStart() {
|
|
|
|
showDialog(Id.dialog.encrypting);
|
|
|
|
startThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
String error = null;
|
|
|
|
Bundle data = new Bundle();
|
|
|
|
Message msg = new Message();
|
2010-07-27 18:41:50 -04:00
|
|
|
|
2010-05-09 15:51:21 -04:00
|
|
|
try {
|
2010-07-09 16:55:17 -04:00
|
|
|
InputData in;
|
2010-05-09 15:51:21 -04:00
|
|
|
OutputStream out;
|
|
|
|
boolean useAsciiArmour = true;
|
|
|
|
long encryptionKeyIds[] = null;
|
|
|
|
long signatureKeyId = 0;
|
2010-05-16 10:20:17 -04:00
|
|
|
int compressionId = 0;
|
2010-07-27 18:41:50 -04:00
|
|
|
boolean signOnly = false;
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
String passPhrase = null;
|
2010-05-13 16:41:32 -04:00
|
|
|
if (mMode.getCurrentView().getId() == R.id.modeSymmetric) {
|
2010-05-09 15:51:21 -04:00
|
|
|
passPhrase = mPassPhrase.getText().toString();
|
|
|
|
if (passPhrase.length() == 0) {
|
|
|
|
passPhrase = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
encryptionKeyIds = mEncryptionKeyIds;
|
|
|
|
signatureKeyId = getSecretKeyId();
|
2010-06-01 10:59:06 -04:00
|
|
|
signOnly = (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0);
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
2010-07-27 18:41:50 -04:00
|
|
|
fillDataSource(signOnly && !mReturnResult);
|
|
|
|
fillDataDestination();
|
|
|
|
|
2010-07-09 16:55:17 -04:00
|
|
|
// streams
|
|
|
|
in = mDataSource.getInputData(this, true);
|
|
|
|
out = mDataDestination.getOutputStream(this);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
2010-07-09 16:55:17 -04:00
|
|
|
if (mEncryptTarget == Id.target.file) {
|
2010-05-09 15:51:21 -04:00
|
|
|
useAsciiArmour = mAsciiArmour.isChecked();
|
2010-05-16 10:20:17 -04:00
|
|
|
compressionId = ((Choice) mFileCompression.getSelectedItem()).getId();
|
2010-05-09 15:51:21 -04:00
|
|
|
} else {
|
|
|
|
useAsciiArmour = true;
|
2010-06-17 09:23:07 -04:00
|
|
|
compressionId = mPreferences.getDefaultMessageCompression();
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
2010-07-09 16:55:17 -04:00
|
|
|
if (mOverrideAsciiArmour) {
|
|
|
|
useAsciiArmour = mAsciiArmourDemand;
|
|
|
|
}
|
|
|
|
|
2010-12-25 09:00:25 -05:00
|
|
|
if (mGenerateSignature) {
|
|
|
|
Apg.generateSignature(this, in, out, useAsciiArmour, mDataSource.isBinary(),
|
|
|
|
getSecretKeyId(),
|
|
|
|
Apg.getCachedPassPhrase(getSecretKeyId()),
|
|
|
|
mPreferences.getDefaultHashAlgorithm(),
|
|
|
|
mPreferences.getForceV3Signatures(),
|
|
|
|
this);
|
|
|
|
} else if (signOnly) {
|
2010-05-13 18:47:19 -04:00
|
|
|
Apg.signText(this, in, out, getSecretKeyId(),
|
2010-05-15 11:19:56 -04:00
|
|
|
Apg.getCachedPassPhrase(getSecretKeyId()),
|
2010-07-27 18:41:50 -04:00
|
|
|
mPreferences.getDefaultHashAlgorithm(),
|
|
|
|
mPreferences.getForceV3Signatures(),
|
|
|
|
this);
|
2010-05-09 15:51:21 -04:00
|
|
|
} else {
|
2010-07-09 16:55:17 -04:00
|
|
|
Apg.encrypt(this, in, out, useAsciiArmour,
|
2010-05-09 15:51:21 -04:00
|
|
|
encryptionKeyIds, signatureKeyId,
|
2010-05-15 11:19:56 -04:00
|
|
|
Apg.getCachedPassPhrase(signatureKeyId), this,
|
2010-06-17 09:23:07 -04:00
|
|
|
mPreferences.getDefaultEncryptionAlgorithm(),
|
|
|
|
mPreferences.getDefaultHashAlgorithm(),
|
2010-07-27 18:41:50 -04:00
|
|
|
compressionId,
|
|
|
|
mPreferences.getForceV3Signatures(),
|
|
|
|
passPhrase);
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
if (mEncryptTarget != Id.target.file) {
|
2010-07-09 16:55:17 -04:00
|
|
|
if (useAsciiArmour) {
|
2010-12-25 09:00:25 -05:00
|
|
|
String extraData = new String(((ByteArrayOutputStream)out).toByteArray());
|
|
|
|
if (mGenerateSignature) {
|
|
|
|
data.putString(Apg.EXTRA_SIGNATURE_TEXT, extraData);
|
|
|
|
} else {
|
|
|
|
data.putString(Apg.EXTRA_ENCRYPTED_MESSAGE, extraData);
|
|
|
|
}
|
2010-07-09 16:55:17 -04:00
|
|
|
} else {
|
2010-12-25 09:00:25 -05:00
|
|
|
byte extraData[] = ((ByteArrayOutputStream)out).toByteArray();
|
|
|
|
if (mGenerateSignature) {
|
|
|
|
data.putByteArray(Apg.EXTRA_SIGNATURE_DATA, extraData);
|
|
|
|
} else {
|
|
|
|
data.putByteArray(Apg.EXTRA_ENCRYPTED_DATA, extraData);
|
|
|
|
}
|
2010-07-09 16:55:17 -04:00
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
2010-05-20 22:47:40 -04:00
|
|
|
error = "" + e;
|
2010-05-09 15:51:21 -04:00
|
|
|
} catch (PGPException e) {
|
2010-05-20 22:47:40 -04:00
|
|
|
error = "" + e;
|
2010-05-09 15:51:21 -04:00
|
|
|
} catch (NoSuchProviderException e) {
|
2010-05-20 22:47:40 -04:00
|
|
|
error = "" + e;
|
2010-05-09 15:51:21 -04:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2010-05-20 22:47:40 -04:00
|
|
|
error = "" + e;
|
2010-05-09 15:51:21 -04:00
|
|
|
} catch (SignatureException e) {
|
2010-05-20 22:47:40 -04:00
|
|
|
error = "" + e;
|
2010-05-09 15:51:21 -04:00
|
|
|
} catch (Apg.GeneralException e) {
|
2010-05-20 22:47:40 -04:00
|
|
|
error = "" + e;
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
2010-09-11 19:21:53 -04:00
|
|
|
data.putInt(Constants.extras.status, Id.message.done);
|
2010-05-09 15:51:21 -04:00
|
|
|
|
|
|
|
if (error != null) {
|
2010-05-31 19:15:20 -04:00
|
|
|
data.putString(Apg.EXTRA_ERROR, error);
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.setData(data);
|
|
|
|
sendMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateView() {
|
|
|
|
if (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0) {
|
2010-05-13 16:41:32 -04:00
|
|
|
mSelectKeysButton.setText(R.string.noKeysSelected);
|
2010-05-09 15:51:21 -04:00
|
|
|
} else if (mEncryptionKeyIds.length == 1) {
|
2010-05-13 16:41:32 -04:00
|
|
|
mSelectKeysButton.setText(R.string.oneKeySelected);
|
2010-05-09 15:51:21 -04:00
|
|
|
} else {
|
|
|
|
mSelectKeysButton.setText("" + mEncryptionKeyIds.length + " " +
|
2010-05-13 16:41:32 -04:00
|
|
|
getResources().getString(R.string.nKeysSelected));
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getSecretKeyId() == 0) {
|
|
|
|
mSign.setChecked(false);
|
|
|
|
mMainUserId.setText("");
|
|
|
|
mMainUserIdRest.setText("");
|
|
|
|
} else {
|
2010-05-13 16:41:32 -04:00
|
|
|
String uid = getResources().getString(R.string.unknownUserId);
|
2010-05-09 15:51:21 -04:00
|
|
|
String uidExtra = "";
|
|
|
|
PGPSecretKeyRing keyRing = Apg.getSecretKeyRing(getSecretKeyId());
|
|
|
|
if (keyRing != null) {
|
|
|
|
PGPSecretKey key = Apg.getMasterKey(keyRing);
|
|
|
|
if (key != null) {
|
|
|
|
String userId = Apg.getMainUserIdSafe(this, key);
|
|
|
|
String chunks[] = userId.split(" <", 2);
|
|
|
|
uid = chunks[0];
|
|
|
|
if (chunks.length > 1) {
|
|
|
|
uidExtra = "<" + chunks[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mMainUserId.setText(uid);
|
|
|
|
mMainUserIdRest.setText(uidExtra);
|
|
|
|
mSign.setChecked(true);
|
|
|
|
}
|
2010-07-24 14:54:40 -04:00
|
|
|
|
|
|
|
updateButtons();
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void selectPublicKeys() {
|
|
|
|
Intent intent = new Intent(this, SelectPublicKeyListActivity.class);
|
2010-06-07 10:21:28 -04:00
|
|
|
Vector<Long> keyIds = new Vector<Long>();
|
|
|
|
if (getSecretKeyId() != 0) {
|
|
|
|
keyIds.add(getSecretKeyId());
|
|
|
|
}
|
|
|
|
if (mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0) {
|
|
|
|
for (int i = 0; i < mEncryptionKeyIds.length; ++i) {
|
|
|
|
keyIds.add(mEncryptionKeyIds[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
long [] initialKeyIds = null;
|
|
|
|
if (keyIds.size() > 0) {
|
|
|
|
initialKeyIds = new long[keyIds.size()];
|
|
|
|
for (int i = 0; i < keyIds.size(); ++i) {
|
|
|
|
initialKeyIds[i] = keyIds.get(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intent.putExtra(Apg.EXTRA_SELECTION, initialKeyIds);
|
2010-05-09 15:51:21 -04:00
|
|
|
startActivityForResult(intent, Id.request.public_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void selectSecretKey() {
|
|
|
|
Intent intent = new Intent(this, SelectSecretKeyListActivity.class);
|
|
|
|
startActivityForResult(intent, Id.request.secret_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
|
switch (requestCode) {
|
|
|
|
case Id.request.filename: {
|
|
|
|
if (resultCode == RESULT_OK && data != null) {
|
|
|
|
String filename = data.getDataString();
|
|
|
|
if (filename != null) {
|
|
|
|
// Get rid of URI prefix:
|
|
|
|
if (filename.startsWith("file://")) {
|
|
|
|
filename = filename.substring(7);
|
|
|
|
}
|
|
|
|
// replace %20 and so on
|
|
|
|
filename = Uri.decode(filename);
|
|
|
|
|
|
|
|
mFilename.setText(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Id.request.output_filename: {
|
|
|
|
if (resultCode == RESULT_OK && data != null) {
|
|
|
|
String filename = data.getDataString();
|
|
|
|
if (filename != null) {
|
|
|
|
// Get rid of URI prefix:
|
|
|
|
if (filename.startsWith("file://")) {
|
|
|
|
filename = filename.substring(7);
|
|
|
|
}
|
|
|
|
// replace %20 and so on
|
|
|
|
filename = Uri.decode(filename);
|
|
|
|
|
|
|
|
FileDialog.setFilename(filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Id.request.secret_keys: {
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
updateView();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Id.request.public_keys: {
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
Bundle bundle = data.getExtras();
|
2010-05-31 19:15:20 -04:00
|
|
|
mEncryptionKeyIds = bundle.getLongArray(Apg.EXTRA_SELECTION);
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
updateView();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void doneCallback(Message msg) {
|
|
|
|
super.doneCallback(msg);
|
|
|
|
|
|
|
|
removeDialog(Id.dialog.encrypting);
|
|
|
|
|
|
|
|
Bundle data = msg.getData();
|
2010-05-31 19:15:20 -04:00
|
|
|
String error = data.getString(Apg.EXTRA_ERROR);
|
2010-05-09 15:51:21 -04:00
|
|
|
if (error != null) {
|
2010-07-24 10:24:42 -04:00
|
|
|
Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT).show();
|
2010-05-09 15:51:21 -04:00
|
|
|
return;
|
2010-05-31 23:20:13 -04:00
|
|
|
}
|
|
|
|
switch (mEncryptTarget) {
|
|
|
|
case Id.target.clipboard: {
|
2010-06-06 07:42:41 -04:00
|
|
|
String message = data.getString(Apg.EXTRA_ENCRYPTED_MESSAGE);
|
2010-05-31 23:20:13 -04:00
|
|
|
ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
|
|
|
clip.setText(message);
|
|
|
|
Toast.makeText(this, R.string.encryptionToClipboardSuccessful,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
break;
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
|
2010-05-31 23:20:13 -04:00
|
|
|
case Id.target.email: {
|
2010-06-02 06:45:26 -04:00
|
|
|
if (mReturnResult) {
|
2010-05-31 23:20:13 -04:00
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.putExtras(data);
|
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
finish();
|
|
|
|
return;
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
|
2010-06-06 07:42:41 -04:00
|
|
|
String message = data.getString(Apg.EXTRA_ENCRYPTED_MESSAGE);
|
2010-05-31 23:20:13 -04:00
|
|
|
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
|
|
|
|
emailIntent.setType("text/plain; charset=utf-8");
|
|
|
|
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
|
|
|
|
if (mSubject != null) {
|
|
|
|
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
|
|
|
|
mSubject);
|
|
|
|
}
|
|
|
|
if (mSendTo != null) {
|
|
|
|
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
|
|
|
|
new String[] { mSendTo });
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
2010-05-31 23:20:13 -04:00
|
|
|
EncryptActivity.this.
|
|
|
|
startActivity(Intent.createChooser(emailIntent,
|
|
|
|
getString(R.string.title_sendEmail)));
|
|
|
|
break;
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
|
2010-05-31 23:20:13 -04:00
|
|
|
case Id.target.file: {
|
|
|
|
Toast.makeText(this, R.string.encryptionSuccessful, Toast.LENGTH_SHORT).show();
|
|
|
|
if (mDeleteAfter.isChecked()) {
|
|
|
|
setDeleteFile(mInputFilename);
|
|
|
|
showDialog(Id.dialog.delete_file);
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
2010-05-31 23:20:13 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
// shouldn't happen
|
|
|
|
break;
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Dialog onCreateDialog(int id) {
|
|
|
|
switch (id) {
|
|
|
|
case Id.dialog.output_filename: {
|
2010-05-13 16:41:32 -04:00
|
|
|
return FileDialog.build(this, getString(R.string.title_encryptToFile),
|
|
|
|
getString(R.string.specifyFileToEncryptTo),
|
2010-05-09 15:51:21 -04:00
|
|
|
mOutputFilename,
|
|
|
|
new FileDialog.OnClickListener() {
|
|
|
|
@Override
|
2010-07-26 17:57:28 -04:00
|
|
|
public void onOkClick(String filename, boolean checked) {
|
2010-05-09 15:51:21 -04:00
|
|
|
removeDialog(Id.dialog.output_filename);
|
|
|
|
mOutputFilename = filename;
|
|
|
|
encryptStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCancelClick() {
|
|
|
|
removeDialog(Id.dialog.output_filename);
|
|
|
|
}
|
|
|
|
},
|
2010-05-13 16:41:32 -04:00
|
|
|
getString(R.string.filemanager_titleSave),
|
|
|
|
getString(R.string.filemanager_btnSave),
|
2010-07-26 17:57:28 -04:00
|
|
|
null,
|
2010-05-09 15:51:21 -04:00
|
|
|
Id.request.output_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onCreateDialog(id);
|
|
|
|
}
|
2010-07-09 16:55:17 -04:00
|
|
|
|
2010-07-27 18:41:50 -04:00
|
|
|
protected void fillDataSource(boolean fixContent) {
|
2010-07-09 16:55:17 -04:00
|
|
|
mDataSource = new DataSource();
|
|
|
|
if (mContentUri != null) {
|
|
|
|
mDataSource.setUri(mContentUri);
|
|
|
|
} else if (mEncryptTarget == Id.target.file) {
|
|
|
|
mDataSource.setUri(mInputFilename);
|
|
|
|
} else {
|
|
|
|
if (mData != null) {
|
|
|
|
mDataSource.setData(mData);
|
|
|
|
} else {
|
2010-07-27 18:41:50 -04:00
|
|
|
String message = mMessage.getText().toString();
|
|
|
|
if (fixContent) {
|
|
|
|
// fix the message a bit, trailing spaces and newlines break stuff,
|
|
|
|
// because GMail sends as HTML and such things fuck up the
|
|
|
|
// signature,
|
|
|
|
// TODO: things like "<" and ">" also fuck up the signature
|
|
|
|
message = message.replaceAll(" +\n", "\n");
|
|
|
|
message = message.replaceAll("\n\n+", "\n\n");
|
|
|
|
message = message.replaceFirst("^\n+", "");
|
|
|
|
// make sure there'll be exactly one newline at the end
|
|
|
|
message = message.replaceFirst("\n*$", "\n");
|
|
|
|
}
|
|
|
|
mDataSource.setText(message);
|
2010-07-09 16:55:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void fillDataDestination() {
|
|
|
|
mDataDestination = new DataDestination();
|
|
|
|
if (mContentUri != null) {
|
|
|
|
mDataDestination.setMode(Id.mode.stream);
|
|
|
|
} else if (mEncryptTarget == Id.target.file) {
|
|
|
|
mDataDestination.setFilename(mOutputFilename);
|
|
|
|
mDataDestination.setMode(Id.mode.file);
|
|
|
|
} else {
|
|
|
|
mDataDestination.setMode(Id.mode.byte_array);
|
|
|
|
}
|
|
|
|
}
|
2010-05-09 15:51:21 -04:00
|
|
|
}
|