aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java23
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java35
2 files changed, 57 insertions, 1 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java
index 0bc3ac0ab..f5b2280c9 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java
@@ -33,6 +33,8 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
+import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel;
+import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType;
import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog;
import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult;
import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult;
@@ -46,6 +48,7 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
public class PgpImportExport {
@@ -56,6 +59,7 @@ public class PgpImportExport {
private Context mContext;
private Progressable mProgressable;
+ private AtomicBoolean mCancelled;
private KeychainServiceListener mKeychainServiceListener;
@@ -72,6 +76,14 @@ public class PgpImportExport {
this.mProviderHelper = providerHelper;
}
+ public PgpImportExport(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) {
+ super();
+ mContext = context;
+ mProgressable = progressable;
+ mProviderHelper = providerHelper;
+ mCancelled = cancelled;
+ }
+
public PgpImportExport(Context context,
Progressable progressable, KeychainServiceListener keychainListener) {
super();
@@ -143,6 +155,11 @@ public class PgpImportExport {
int position = 0;
double progSteps = 100.0 / num;
for (ParcelableKeyRing entry : new IterableIterator<ParcelableKeyRing>(entries)) {
+ // Has this action been cancelled? If so, don't proceed any further
+ if (mCancelled != null && mCancelled.get()) {
+ break;
+ }
+
try {
UncachedKeyRing key = UncachedKeyRing.decodeFromData(entry.getBytes());
@@ -208,9 +225,13 @@ public class PgpImportExport {
}
}
if (log.containsWarnings()) {
- resultType |= ImportKeyResult.RESULT_WITH_WARNINGS;
+ resultType |= ImportKeyResult.RESULT_WARNINGS;
}
}
+ if (mCancelled != null && mCancelled.get()) {
+ log.add(LogLevel.CANCELLED, LogType.MSG_OPERATION_CANCELLED, 0);
+ resultType |= ImportKeyResult.RESULT_CANCELLED;
+ }
return new ImportKeyResult(resultType, log, newKeys, oldKeys, badKeys, secret);
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java
index 967a7caa9..6b1433cca 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java
@@ -73,6 +73,7 @@ import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.Stack;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
* This class is the single place where ALL operations that actually modify a PGP public or secret
@@ -85,6 +86,7 @@ import java.util.Stack;
*/
public class PgpKeyOperation {
private Stack<Progressable> mProgress;
+ private AtomicBoolean mCancelled;
// most preferred is first
private static final int[] PREFERRED_SYMMETRIC_ALGORITHMS = new int[]{
@@ -134,6 +136,15 @@ public class PgpKeyOperation {
}
}
+ public PgpKeyOperation(Progressable progress, AtomicBoolean cancelled) {
+ this(progress);
+ mCancelled = cancelled;
+ }
+
+ private boolean checkCancelled() {
+ return mCancelled != null && mCancelled.get();
+ }
+
private void subProgressPush(int from, int to) {
if (mProgress == null) {
return;
@@ -450,6 +461,12 @@ public class PgpKeyOperation {
try {
+ // Check if we were cancelled
+ if (checkCancelled()) {
+ log.add(LogLevel.CANCELLED, LogType.MSG_OPERATION_CANCELLED, indent);
+ return new EditKeyResult(EditKeyResult.RESULT_CANCELLED, log, null);
+ }
+
{ // work on master secret key
PGPPublicKey modifiedPublicKey = masterPublicKey;
@@ -640,6 +657,12 @@ public class PgpKeyOperation {
}
+ // Check if we were cancelled - again
+ if (checkCancelled()) {
+ log.add(LogLevel.CANCELLED, LogType.MSG_OPERATION_CANCELLED, indent);
+ return new EditKeyResult(EditKeyResult.RESULT_CANCELLED, log, null);
+ }
+
// 4a. For each subkey change, generate new subkey binding certificate
subProgressPush(50, 60);
for (int i = 0; i < saveParcel.mChangeSubKeys.size(); i++) {
@@ -750,6 +773,12 @@ public class PgpKeyOperation {
subProgressPush(70, 90);
for (int i = 0; i < saveParcel.mAddSubKeys.size(); i++) {
+ // Check if we were cancelled - again. This operation is expensive so we do it each loop.
+ if (checkCancelled()) {
+ log.add(LogLevel.CANCELLED, LogType.MSG_OPERATION_CANCELLED, indent);
+ return new EditKeyResult(EditKeyResult.RESULT_CANCELLED, log, null);
+ }
+
progress(R.string.progress_modify_subkeyadd, (i-1) * (100 / saveParcel.mAddSubKeys.size()));
SaveKeyringParcel.SubkeyAdd add = saveParcel.mAddSubKeys.get(i);
log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent,
@@ -806,6 +835,12 @@ public class PgpKeyOperation {
}
subProgressPop();
+ // Check if we were cancelled - again. This operation is expensive so we do it each loop.
+ if (checkCancelled()) {
+ log.add(LogLevel.CANCELLED, LogType.MSG_OPERATION_CANCELLED, indent);
+ return new EditKeyResult(EditKeyResult.RESULT_CANCELLED, log, null);
+ }
+
// 6. If requested, change passphrase
if (saveParcel.mNewPassphrase != null) {
progress(R.string.progress_modify_passphrase, 90);