diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/BaseOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/BaseOperation.java new file mode 100644 index 000000000..811df7563 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/BaseOperation.java @@ -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(); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/ImportExportOperation.java similarity index 89% rename from OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java rename to OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/ImportExportOperation.java index 9b21b49ce..32151c4d9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/ImportExportOperation.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package org.sufficientlysecure.keychain.pgp; +package org.sufficientlysecure.keychain.pgp.ops; import android.content.Context; import android.os.Bundle; @@ -29,6 +29,11 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.keyimport.Keyserver.AddKeyException; 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.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; @@ -48,45 +53,14 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.concurrent.atomic.AtomicBoolean; -public class PgpImportExport { +public class ImportExportOperation extends BaseOperation { - private Context mContext; - private Progressable mProgressable; - 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 ImportExportOperation(Context context, ProviderHelper providerHelper, Progressable progressable) { + super(context, providerHelper, progressable); } - public PgpImportExport(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) { - super(); - 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 ImportExportOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) { + super(context, providerHelper, progressable, cancelled); } public void uploadKeyRingToServer(HkpKeyserver server, CanonicalizedPublicKeyRing keyring) throws AddKeyException { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpCertifyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/PgpCertifyOperation.java similarity index 88% rename from OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpCertifyOperation.java rename to OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/PgpCertifyOperation.java index b0c801a93..63dabc8db 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpCertifyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/ops/PgpCertifyOperation.java @@ -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.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.provider.ProviderHelper; 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 java.util.ArrayList; -import java.util.Arrays; import java.util.concurrent.atomic.AtomicBoolean; -public class PgpCertifyOperation { +public class PgpCertifyOperation extends BaseOperation { - private AtomicBoolean mCancelled; - - private ProviderHelper mProviderHelper; - - public PgpCertifyOperation(ProviderHelper providerHelper, AtomicBoolean cancelled) { - mProviderHelper = providerHelper; - - mCancelled = cancelled; - } - - private boolean checkCancelled() { - return mCancelled != null && mCancelled.get(); + public PgpCertifyOperation(Context context, ProviderHelper providerHelper, Progressable progressable, AtomicBoolean cancelled) { + super(context, providerHelper, progressable, cancelled); } public CertifyResult certify(CertifyActionsParcel parcel, String passphrase) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 273f9c75f..b99a06ce0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -41,7 +41,7 @@ import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey.SecretKeyType; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.KeyRing; 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.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; @@ -1090,7 +1090,7 @@ public class ProviderHelper { // 3. Re-Import secret keyrings from cache if (numSecrets > 0) { - new PgpImportExport(mContext, this, + new ImportExportOperation(mContext, this, new ProgressFixedScaler(progress, 10, 25, 100, R.string.progress_con_reimport)) .importKeyRings(itSecrets, numSecrets); } else { @@ -1116,7 +1116,7 @@ public class ProviderHelper { // 4. Re-Import public keyrings from cache if (numPublics > 0) { - new PgpImportExport(mContext, this, + new ImportExportOperation(mContext, this, new ProgressFixedScaler(progress, 25, 99, 100, R.string.progress_con_reimport)) .importKeyRings(itPublics, numPublics); } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 90091ce35..14c72970f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -29,7 +29,7 @@ import android.os.RemoteException; import org.sufficientlysecure.keychain.Constants; 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.provider.ProviderHelper.NotFoundException; 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.service.results.DecryptVerifyResult; 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.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; @@ -268,7 +268,7 @@ public class KeychainIntentService extends IntentService implements Progressable } 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); sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); @@ -610,8 +610,8 @@ public class KeychainIntentService extends IntentService implements Progressable outStream = getContentResolver().openOutputStream(outputUri); } - PgpImportExport pgpImportExport = new PgpImportExport(this, new ProviderHelper(this), this); - Bundle resultData = pgpImportExport + ImportExportOperation importExportOperation = new ImportExportOperation(this, new ProviderHelper(this), this); + Bundle resultData = importExportOperation .exportKeyRings(publicMasterKeyIds, secretMasterKeyIds, outStream); if (mActionCanceled.get() && outputFile != null) { @@ -644,9 +644,9 @@ public class KeychainIntentService extends IntentService implements Progressable } ProviderHelper providerHelper = new ProviderHelper(this); - PgpImportExport pgpImportExport = new PgpImportExport( + ImportExportOperation importExportOperation = new ImportExportOperation( this, providerHelper, this, mActionCanceled); - ImportKeyResult result = pgpImportExport.importKeyRings(entries, numEntries); + ImportKeyResult result = importExportOperation.importKeyRings(entries, numEntries); // we do this even on failure or cancellation! if (result.mSecret > 0) { @@ -761,10 +761,10 @@ public class KeychainIntentService extends IntentService implements Progressable ProviderHelper providerHelper = new ProviderHelper(this); CanonicalizedPublicKeyRing keyring = providerHelper.getCanonicalizedPublicKeyRing(dataUri); - PgpImportExport pgpImportExport = new PgpImportExport(this, new ProviderHelper(this), this); + ImportExportOperation importExportOperation = new ImportExportOperation(this, new ProviderHelper(this), this); try { - pgpImportExport.uploadKeyRingToServer(server, keyring); + importExportOperation.uploadKeyRingToServer(server, keyring); } catch (Keyserver.AddKeyException e) { throw new PgpGeneralException("Unable to export key to selected server"); }