move operations into ops package, introduce BaseOperation class

This commit is contained in:
Vincent Breitmoser 2014-10-10 19:27:33 +02:00
parent 21ea040910
commit 45dcc7d070
5 changed files with 89 additions and 64 deletions

View File

@ -0,0 +1,55 @@
package org.sufficientlysecure.keychain.pgp.ops;
import android.content.Context;
import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import java.util.concurrent.atomic.AtomicBoolean;
public class BaseOperation {
final public Context mContext;
final public Progressable mProgressable;
final public AtomicBoolean mCancelled;
final public ProviderHelper mProviderHelper;
// TODO do we really need the context in these operations?
public BaseOperation(Context context, ProviderHelper providerHelper, Progressable progressable) {
this.mContext = context;
this.mProgressable = progressable;
this.mProviderHelper = providerHelper;
mCancelled = null;
}
public BaseOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) {
mContext = context;
mProgressable = progressable;
mProviderHelper = providerHelper;
mCancelled = cancelled;
}
public void updateProgress(int message, int current, int total) {
if (mProgressable != null) {
mProgressable.setProgress(message, current, total);
}
}
public void updateProgress(String message, int current, int total) {
if (mProgressable != null) {
mProgressable.setProgress(message, current, total);
}
}
public void updateProgress(int current, int total) {
if (mProgressable != null) {
mProgressable.setProgress(current, total);
}
}
protected boolean checkCancelled() {
return mCancelled != null && mCancelled.get();
}
}

View File

@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.sufficientlysecure.keychain.pgp; package org.sufficientlysecure.keychain.pgp.ops;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
@ -29,6 +29,11 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver;
import org.sufficientlysecure.keychain.keyimport.Keyserver.AddKeyException; import org.sufficientlysecure.keychain.keyimport.Keyserver.AddKeyException;
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper;
@ -48,45 +53,14 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class PgpImportExport { public class ImportExportOperation extends BaseOperation {
private Context mContext; public ImportExportOperation(Context context, ProviderHelper providerHelper, Progressable progressable) {
private Progressable mProgressable; super(context, providerHelper, progressable);
private AtomicBoolean mCancelled;
private ProviderHelper mProviderHelper;
public PgpImportExport(Context context, ProviderHelper providerHelper, Progressable progressable) {
super();
this.mContext = context;
this.mProgressable = progressable;
this.mProviderHelper = providerHelper;
} }
public PgpImportExport(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) { public ImportExportOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) {
super(); super(context, providerHelper, progressable, cancelled);
mContext = context;
mProgressable = progressable;
mProviderHelper = providerHelper;
mCancelled = cancelled;
}
public void updateProgress(int message, int current, int total) {
if (mProgressable != null) {
mProgressable.setProgress(message, current, total);
}
}
public void updateProgress(String message, int current, int total) {
if (mProgressable != null) {
mProgressable.setProgress(message, current, total);
}
}
public void updateProgress(int current, int total) {
if (mProgressable != null) {
mProgressable.setProgress(current, total);
}
} }
public void uploadKeyRingToServer(HkpKeyserver server, CanonicalizedPublicKeyRing keyring) throws AddKeyException { public void uploadKeyRingToServer(HkpKeyserver server, CanonicalizedPublicKeyRing keyring) throws AddKeyException {

View File

@ -1,7 +1,14 @@
package org.sufficientlysecure.keychain.pgp; package org.sufficientlysecure.keychain.pgp.ops;
import android.content.Context;
import org.spongycastle.openpgp.PGPException; import org.spongycastle.openpgp.PGPException;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException;
@ -15,23 +22,12 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class PgpCertifyOperation { public class PgpCertifyOperation extends BaseOperation {
private AtomicBoolean mCancelled; public PgpCertifyOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) {
super(context, providerHelper, progressable, cancelled);
private ProviderHelper mProviderHelper;
public PgpCertifyOperation(ProviderHelper providerHelper, AtomicBoolean cancelled) {
mProviderHelper = providerHelper;
mCancelled = cancelled;
}
private boolean checkCancelled() {
return mCancelled != null && mCancelled.get();
} }
public CertifyResult certify(CertifyActionsParcel parcel, String passphrase) { public CertifyResult certify(CertifyActionsParcel parcel, String passphrase) {

View File

@ -41,7 +41,7 @@ import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType;
import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing;
import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.KeyRing;
import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpImportExport; import org.sufficientlysecure.keychain.pgp.ops.ImportExportOperation;
import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing;
import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
@ -1090,7 +1090,7 @@ public class ProviderHelper {
// 3. Re-Import secret keyrings from cache // 3. Re-Import secret keyrings from cache
if (numSecrets > 0) { if (numSecrets > 0) {
new PgpImportExport(mContext, this, new ImportExportOperation(mContext, this,
new ProgressFixedScaler(progress, 10, 25, 100, R.string.progress_con_reimport)) new ProgressFixedScaler(progress, 10, 25, 100, R.string.progress_con_reimport))
.importKeyRings(itSecrets, numSecrets); .importKeyRings(itSecrets, numSecrets);
} else { } else {
@ -1116,7 +1116,7 @@ public class ProviderHelper {
// 4. Re-Import public keyrings from cache // 4. Re-Import public keyrings from cache
if (numPublics > 0) { if (numPublics > 0) {
new PgpImportExport(mContext, this, new ImportExportOperation(mContext, this,
new ProgressFixedScaler(progress, 25, 99, 100, R.string.progress_con_reimport)) new ProgressFixedScaler(progress, 25, 99, 100, R.string.progress_con_reimport))
.importKeyRings(itPublics, numPublics); .importKeyRings(itPublics, numPublics);
} else { } else {

View File

@ -29,7 +29,7 @@ import android.os.RemoteException;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.PgpCertifyOperation; import org.sufficientlysecure.keychain.pgp.ops.PgpCertifyOperation;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException; import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException;
import org.sufficientlysecure.keychain.service.results.CertifyResult; import org.sufficientlysecure.keychain.service.results.CertifyResult;
@ -47,7 +47,7 @@ import org.sufficientlysecure.keychain.pgp.PassphraseCacheInterface;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify;
import org.sufficientlysecure.keychain.service.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.service.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpImportExport; import org.sufficientlysecure.keychain.pgp.ops.ImportExportOperation;
import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpKeyOperation;
import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt;
import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.Progressable;
@ -268,7 +268,7 @@ public class KeychainIntentService extends IntentService implements Progressable
} }
ProviderHelper providerHelper = new ProviderHelper(this); ProviderHelper providerHelper = new ProviderHelper(this);
PgpCertifyOperation op = new PgpCertifyOperation(providerHelper, mActionCanceled); PgpCertifyOperation op = new PgpCertifyOperation(this, providerHelper, this, mActionCanceled);
CertifyResult result = op.certify(parcel, passphrase); CertifyResult result = op.certify(parcel, passphrase);
sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result);
@ -610,8 +610,8 @@ public class KeychainIntentService extends IntentService implements Progressable
outStream = getContentResolver().openOutputStream(outputUri); outStream = getContentResolver().openOutputStream(outputUri);
} }
PgpImportExport pgpImportExport = new PgpImportExport(this, new ProviderHelper(this), this); ImportExportOperation importExportOperation = new ImportExportOperation(this, new ProviderHelper(this), this);
Bundle resultData = pgpImportExport Bundle resultData = importExportOperation
.exportKeyRings(publicMasterKeyIds, secretMasterKeyIds, outStream); .exportKeyRings(publicMasterKeyIds, secretMasterKeyIds, outStream);
if (mActionCanceled.get() && outputFile != null) { if (mActionCanceled.get() && outputFile != null) {
@ -644,9 +644,9 @@ public class KeychainIntentService extends IntentService implements Progressable
} }
ProviderHelper providerHelper = new ProviderHelper(this); ProviderHelper providerHelper = new ProviderHelper(this);
PgpImportExport pgpImportExport = new PgpImportExport( ImportExportOperation importExportOperation = new ImportExportOperation(
this, providerHelper, this, mActionCanceled); this, providerHelper, this, mActionCanceled);
ImportKeyResult result = pgpImportExport.importKeyRings(entries, numEntries); ImportKeyResult result = importExportOperation.importKeyRings(entries, numEntries);
// we do this even on failure or cancellation! // we do this even on failure or cancellation!
if (result.mSecret > 0) { if (result.mSecret > 0) {
@ -761,10 +761,10 @@ public class KeychainIntentService extends IntentService implements Progressable
ProviderHelper providerHelper = new ProviderHelper(this); ProviderHelper providerHelper = new ProviderHelper(this);
CanonicalizedPublicKeyRing keyring = providerHelper.getCanonicalizedPublicKeyRing(dataUri); CanonicalizedPublicKeyRing keyring = providerHelper.getCanonicalizedPublicKeyRing(dataUri);
PgpImportExport pgpImportExport = new PgpImportExport(this, new ProviderHelper(this), this); ImportExportOperation importExportOperation = new ImportExportOperation(this, new ProviderHelper(this), this);
try { try {
pgpImportExport.uploadKeyRingToServer(server, keyring); importExportOperation.uploadKeyRingToServer(server, keyring);
} catch (Keyserver.AddKeyException e) { } catch (Keyserver.AddKeyException e) {
throw new PgpGeneralException("Unable to export key to selected server"); throw new PgpGeneralException("Unable to export key to selected server");
} }