import: make operation cancelable (again), make "update all" cancelable
(also, use unbounded blocking queue to fix update of more than 10 keys)
This commit is contained in:
@@ -446,7 +446,6 @@ public class HkpKeyserver extends Keyserver {
|
||||
* Tries to find a server responsible for a given domain
|
||||
*
|
||||
* @return A responsible Keyserver or null if not found.
|
||||
* TODO: Add proxy functionality
|
||||
*/
|
||||
public static HkpKeyserver resolve(String domain, Proxy proxy) {
|
||||
try {
|
||||
|
||||
@@ -28,7 +28,7 @@ import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorCompletionService;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -262,12 +262,6 @@ public class ImportOperation extends BaseOperation<ImportKeyringParcel> {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Another check if we have been cancelled
|
||||
if (checkCancelled()) {
|
||||
cancelled = true;
|
||||
break;
|
||||
}
|
||||
|
||||
SaveKeyringResult result;
|
||||
// synchronizing prevents https://github.com/open-keychain/open-keychain/issues/1221
|
||||
// and https://github.com/open-keychain/open-keychain/issues/1480
|
||||
@@ -366,13 +360,15 @@ public class ImportOperation extends BaseOperation<ImportKeyringParcel> {
|
||||
}
|
||||
}
|
||||
|
||||
// Final log entry, it's easier to do this individually
|
||||
if ((newKeys > 0 || updatedKeys > 0) && badKeys > 0) {
|
||||
log.add(LogType.MSG_IMPORT_PARTIAL, 1);
|
||||
} else if (newKeys > 0 || updatedKeys > 0) {
|
||||
log.add(LogType.MSG_IMPORT_SUCCESS, 1);
|
||||
} else {
|
||||
log.add(LogType.MSG_IMPORT_ERROR, 1);
|
||||
if (!cancelled) {
|
||||
// Final log entry, it's easier to do this individually
|
||||
if ((newKeys > 0 || updatedKeys > 0) && badKeys > 0) {
|
||||
log.add(LogType.MSG_IMPORT_PARTIAL, 1);
|
||||
} else if (newKeys > 0 || updatedKeys > 0) {
|
||||
log.add(LogType.MSG_IMPORT_SUCCESS, 1);
|
||||
} else {
|
||||
log.add(LogType.MSG_IMPORT_ERROR, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return new ImportKeyResult(resultType, log, newKeys, updatedKeys, badKeys, secret,
|
||||
@@ -423,7 +419,7 @@ public class ImportOperation extends BaseOperation<ImportKeyringParcel> {
|
||||
final ProgressScaler ignoreProgressable = new ProgressScaler();
|
||||
|
||||
ExecutorService importExecutor = new ThreadPoolExecutor(0, MAX_THREADS, 30L, TimeUnit.SECONDS,
|
||||
new SynchronousQueue<Runnable>());
|
||||
new LinkedBlockingQueue<Runnable>());
|
||||
|
||||
ExecutorCompletionService<ImportKeyResult> importCompletionService =
|
||||
new ExecutorCompletionService<>(importExecutor);
|
||||
@@ -438,6 +434,10 @@ public class ImportOperation extends BaseOperation<ImportKeyringParcel> {
|
||||
@Override
|
||||
public ImportKeyResult call() {
|
||||
|
||||
if (checkCancelled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<ParcelableKeyRing> list = new ArrayList<>();
|
||||
list.add(pkRing);
|
||||
|
||||
@@ -481,6 +481,7 @@ public class ImportOperation extends BaseOperation<ImportKeyringParcel> {
|
||||
private int mUpdatedKeys = 0;
|
||||
private int mSecret = 0;
|
||||
private int mResultType = 0;
|
||||
private boolean mHasCancelledResult;
|
||||
|
||||
/**
|
||||
* Accumulates keyring imports and updates the progressable whenever a new key is imported.
|
||||
@@ -501,11 +502,22 @@ public class ImportOperation extends BaseOperation<ImportKeyringParcel> {
|
||||
public void accumulateKeyImport(ImportKeyResult result) {
|
||||
mImportedKeys++;
|
||||
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mProgressable != null) {
|
||||
mProgressable.setProgress(mImportedKeys, mTotalKeys);
|
||||
}
|
||||
|
||||
mImportLog.addAll(result.getLog().toList());//accumulates log
|
||||
boolean notCancelledOrFirstCancelled = !result.cancelled() || !mHasCancelledResult;
|
||||
if (notCancelledOrFirstCancelled) {
|
||||
mImportLog.addAll(result.getLog().toList()); //accumulates log
|
||||
if (result.cancelled()) {
|
||||
mHasCancelledResult = true;
|
||||
}
|
||||
}
|
||||
|
||||
mBadKeys += result.mBadKeys;
|
||||
mNewKeys += result.mNewKeys;
|
||||
mUpdatedKeys += result.mUpdatedKeys;
|
||||
|
||||
@@ -559,8 +559,8 @@ public class KeyListFragment extends LoaderFragment
|
||||
mKeyserver = cloudPrefs.keyserver;
|
||||
}
|
||||
|
||||
mImportOpHelper = new CryptoOperationHelper<>(1, this,
|
||||
this, R.string.progress_updating);
|
||||
mImportOpHelper = new CryptoOperationHelper<>(1, this, this, R.string.progress_updating);
|
||||
mImportOpHelper.setProgressCancellable(true);
|
||||
mImportOpHelper.cryptoOperation();
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
||||
public static final int REQUEST_CODE_RETRY_UPLOAD = 4;
|
||||
|
||||
private Integer mProgressMessageResource;
|
||||
private boolean mCancellable = false;
|
||||
|
||||
private FragmentActivity mActivity;
|
||||
private Fragment mFragment;
|
||||
@@ -118,6 +119,10 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
||||
mProgressMessageResource = id;
|
||||
}
|
||||
|
||||
public void setProgressCancellable(boolean cancellable) {
|
||||
mCancellable = cancellable;
|
||||
}
|
||||
|
||||
private void initiateInputActivity(RequiredInputParcel requiredInput,
|
||||
CryptoInputParcel cryptoInputParcel) {
|
||||
|
||||
@@ -311,7 +316,7 @@ public class CryptoOperationHelper<T extends Parcelable, S extends OperationResu
|
||||
if (mProgressMessageResource != null) {
|
||||
saveHandler.showProgressDialog(
|
||||
activity.getString(mProgressMessageResource),
|
||||
ProgressDialog.STYLE_HORIZONTAL, false);
|
||||
ProgressDialog.STYLE_HORIZONTAL, mCancellable);
|
||||
}
|
||||
|
||||
activity.startService(intent);
|
||||
|
||||
Reference in New Issue
Block a user