Refactoring

pull/146/head
M66B 7 years ago
parent 7d21a147b1
commit 435a73fcab

@ -26,6 +26,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.net.Uri; import android.net.Uri;
@ -55,10 +56,10 @@ import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -70,8 +71,11 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Properties;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -91,9 +95,9 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
private ListView drawerList; private ListView drawerList;
private ActionBarDrawerToggle drawerToggle; private ActionBarDrawerToggle drawerToggle;
private long message = -1;
private long attachment = -1; private long attachment = -1;
private long decryptId = -1;
private File decryptFile = null;
private OpenPgpServiceConnection pgpService; private OpenPgpServiceConnection pgpService;
private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes private static final int ATTACHMENT_BUFFER_SIZE = 8192; // bytes
@ -894,51 +898,12 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
} }
private void onDecrypt(Intent intent) { private void onDecrypt(Intent intent) {
Bundle args = new Bundle();
args.putLong("id", intent.getLongExtra("id", -1));
new SimpleTask<File>() {
@Override
protected File onLoad(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
try {
db.beginTransaction();
for (EntityAttachment attachment : db.attachment().getAttachments(id))
if (attachment.available && "encrypted.asc".equals(attachment.name))
return EntityAttachment.getFile(context, attachment.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return null;
}
@Override
protected void onLoaded(Bundle args, File file) {
if (file != null)
try {
if (!pgpService.isBound())
throw new IllegalArgumentException(getString(R.string.title_no_openpgp));
Intent data = new Intent(); Intent data = new Intent();
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{args.getString("to")}); data.putExtra(OpenPgpApi.EXTRA_USER_IDS, new String[]{intent.getStringExtra("to")});
data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); data.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true);
decrypt(data, args.getLong("id"), file); decrypt(data, intent.getLongExtra("id", -1));
} catch (Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ex);
}
}
}.load(this, args);
} }
private void onShowPro(Intent intent) { private void onShowPro(Intent intent) {
@ -947,63 +912,78 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
private void decrypt(Intent data, final long id, final File file) throws FileNotFoundException { private void decrypt(Intent data, long id) {
final OpenPgpApi api = new OpenPgpApi(this, pgpService.getService());
final FileInputStream msg = new FileInputStream(file);
final ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
api.executeApiAsync(data, msg, decrypted, new OpenPgpApi.IOpenPgpCallback() {
@Override
public void onReturn(Intent result) {
try {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
args.putParcelable("data", data);
new SimpleTask<Void>() { new SimpleTask<PendingIntent>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) throws Throwable { protected PendingIntent onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id"); long id = args.getLong("id");
Intent data = args.getParcelable("data");
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id); for (EntityAttachment attachment : db.attachment().getAttachments(id))
message.write(context, decrypted.toString("UTF-8")); if (attachment.available && "encrypted.asc".equals(attachment.name)) {
if (!pgpService.isBound())
throw new IllegalArgumentException(getString(R.string.title_no_openpgp));
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
FileInputStream encrypted = new FileInputStream(EntityAttachment.getFile(context, attachment.id));
ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
Intent result = api.executeApi(data, encrypted, decrypted);
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
ByteArrayInputStream is = new ByteArrayInputStream(decrypted.toByteArray());
Properties props = MessageHelper.getSessionProperties(Helper.AUTH_TYPE_PASSWORD, false);
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession, is);
MessageHelper helper = new MessageHelper(imessage);
EntityMessage m = db.message().getMessage(id);
m.write(context, helper.getHtml());
db.message().setMessageStored(id, new Date().getTime()); db.message().setMessageStored(id, new Date().getTime());
break;
return null; case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
} message = id;
return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
@Override case OpenPgpApi.RESULT_CODE_ERROR:
protected void onException(Bundle args, Throwable ex) { OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
Helper.unexpectedError(ActivityView.this, ex); throw new IllegalArgumentException(error.getMessage());
} }
}.load(ActivityView.this, args);
break; break;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: return null;
decryptId = id; }
decryptFile = file;
PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); @Override
protected void onLoaded(Bundle args, PendingIntent pi) {
if (pi != null)
try {
startIntentSenderForResult( startIntentSenderForResult(
pi.getIntentSender(), pi.getIntentSender(),
ActivityView.REQUEST_DECRYPT, ActivityView.REQUEST_DECRYPT,
null, 0, 0, 0, null); null, 0, 0, 0, null);
break; } catch (IntentSender.SendIntentException ex) {
Helper.unexpectedError(ActivityView.this, ex);
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException(error.getMessage());
} }
} catch (Throwable ex) { }
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException) if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else else
Helper.unexpectedError(ActivityView.this, ex); Helper.unexpectedError(ActivityView.this, ex);
} }
} }.load(ActivityView.this, args);
});
} }
@Override @Override
@ -1075,14 +1055,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
} }
} else if (requestCode == REQUEST_DECRYPT) { } else if (requestCode == REQUEST_DECRYPT) {
if (data != null) if (data != null)
try { decrypt(data, message);
decrypt(data, decryptId, decryptFile);
} catch (Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ex);
}
} }
} }
} }

@ -25,6 +25,7 @@ import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Typeface; import android.graphics.Typeface;
@ -65,29 +66,38 @@ import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import org.jsoup.Jsoup;
import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Properties;
import javax.mail.Address; import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.MessageRemovedException; import javax.mail.MessageRemovedException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.AddressException; import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -482,9 +492,6 @@ public class FragmentCompose extends FragmentEx {
private void onEncrypt() { private void onEncrypt() {
try { try {
if (!pgpService.isBound())
throw new IllegalArgumentException(getString(R.string.title_no_openpgp));
String to = etTo.getText().toString(); String to = etTo.getText().toString();
InternetAddress ato[] = (TextUtils.isEmpty(to) ? new InternetAddress[0] : InternetAddress.parse(to)); InternetAddress ato[] = (TextUtils.isEmpty(to) ? new InternetAddress[0] : InternetAddress.parse(to));
if (ato.length == 0) if (ato.length == 0)
@ -508,36 +515,54 @@ public class FragmentCompose extends FragmentEx {
} }
} }
private void encrypt(final Intent data) throws IOException { private void encrypt(Intent data) {
final OpenPgpApi api = new OpenPgpApi(getContext(), pgpService.getService());
final FileInputStream msg = new FileInputStream(EntityMessage.getFile(getContext(), working));
final ByteArrayOutputStream encrypted = new ByteArrayOutputStream();
final Bundle args = new Bundle(); final Bundle args = new Bundle();
args.putLong("id", working); args.putLong("id", working);
args.putParcelable("data", data);
api.executeApiAsync(data, msg, encrypted, new OpenPgpApi.IOpenPgpCallback() { new SimpleTask<PendingIntent>() {
@Override @Override
public void onReturn(Intent result) { protected PendingIntent onLoad(Context context, Bundle args) throws Throwable {
Log.i(Helper.TAG, "Pgp result=" + result); long id = args.getLong("id");
Bundle extras = result.getExtras(); Intent data = args.getParcelable("data");
for (String key : extras.keySet())
Log.i(Helper.TAG, key + "=" + extras.get(key)); String body = EntityMessage.read(getContext(), id);
BodyPart plain = new MimeBodyPart();
plain.setContent(Jsoup.parse(body).text(), "text/plain; charset=" + Charset.defaultCharset().name());
BodyPart html = new MimeBodyPart();
html.setContent(body, "text/html; charset=" + Charset.defaultCharset().name());
Multipart alternative = new MimeMultipart("alternative");
alternative.addBodyPart(plain);
alternative.addBodyPart(html);
Properties props = MessageHelper.getSessionProperties(Helper.AUTH_TYPE_PASSWORD, false);
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession);
imessage.setContent(alternative);
ByteArrayOutputStream os = new ByteArrayOutputStream();
imessage.writeTo(os);
ByteArrayInputStream decrypted = new ByteArrayInputStream(os.toByteArray());
ByteArrayOutputStream encrypted = new ByteArrayOutputStream();
if (!pgpService.isBound())
throw new IllegalArgumentException(getString(R.string.title_no_openpgp));
OpenPgpApi api = new OpenPgpApi(getContext(), pgpService.getService());
Intent result = api.executeApi(data, decrypted, encrypted);
try {
switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
case OpenPgpApi.RESULT_CODE_SUCCESS: case OpenPgpApi.RESULT_CODE_SUCCESS:
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
EntityAttachment attachment1 = new EntityAttachment(); EntityAttachment attachment1 = new EntityAttachment();
EntityAttachment attachment2 = new EntityAttachment(); EntityAttachment attachment2 = new EntityAttachment();
Intent keyRequest = new Intent(); Intent keyRequest = new Intent();
keyRequest.setAction(OpenPgpApi.ACTION_DETACHED_SIGN); keyRequest.setAction(OpenPgpApi.ACTION_DETACHED_SIGN);
keyRequest.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1)); keyRequest.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, data.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, -1));
Intent keyData = api.executeApi(keyRequest, msg, null); Intent keyData = api.executeApi(keyRequest, decrypted, null);
int r = keyData.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR); int r = keyData.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
if (r != OpenPgpApi.RESULT_CODE_SUCCESS) { if (r != OpenPgpApi.RESULT_CODE_SUCCESS) {
OpenPgpError error = keyData.getParcelableExtra(OpenPgpApi.RESULT_ERROR); OpenPgpError error = keyData.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
@ -563,10 +588,10 @@ public class FragmentCompose extends FragmentEx {
OutputStream os1 = null; OutputStream os1 = null;
try { try {
os1 = new BufferedOutputStream(new FileOutputStream(file1)); os1 = new BufferedOutputStream(new FileOutputStream(file1));
byte[] data = encrypted.toByteArray(); byte[] bytes = encrypted.toByteArray();
os1.write(data); os1.write(bytes);
attachment1.size = data.length; attachment1.size = bytes.length;
attachment1.progress = null; attachment1.progress = null;
attachment1.available = true; attachment1.available = true;
db.attachment().updateAttachment(attachment1); db.attachment().updateAttachment(attachment1);
@ -602,37 +627,40 @@ public class FragmentCompose extends FragmentEx {
db.endTransaction(); db.endTransaction();
} }
return null; break;
}
@Override case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
protected void onException(Bundle args, Throwable ex) { return result.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
Helper.unexpectedError(getContext(), ex);
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException(error.getMessage());
} }
}.load(FragmentCompose.this, args);
break; return null;
}
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: @Override
PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); protected void onLoaded(Bundle args, PendingIntent pi) {
if (pi != null)
try {
startIntentSenderForResult( startIntentSenderForResult(
pi.getIntentSender(), pi.getIntentSender(),
ActivityCompose.REQUEST_ENCRYPT, ActivityCompose.REQUEST_ENCRYPT,
null, 0, 0, 0, null); null, 0, 0, 0, null);
break; } catch (IntentSender.SendIntentException ex) {
Helper.unexpectedError(getContext(), ex);
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
throw new IllegalArgumentException(error.getMessage());
} }
} catch (Throwable ex) { }
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException) if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show(); Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else else
Helper.unexpectedError(getContext(), ex); Helper.unexpectedError(getContext(), ex);
} }
} }.load(this, args);
});
} }
@Override @Override
@ -646,15 +674,9 @@ public class FragmentCompose extends FragmentEx {
if (data != null) if (data != null)
handleAddAttachment(data, false); handleAddAttachment(data, false);
} else if (requestCode == ActivityCompose.REQUEST_ENCRYPT) { } else if (requestCode == ActivityCompose.REQUEST_ENCRYPT) {
if (data != null) if (data != null) {
try {
data.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); data.setAction(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT);
encrypt(data); encrypt(data);
} catch (Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(getContext(), ex);
} }
} else { } else {
if (data != null) if (data != null)

Loading…
Cancel
Save