aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2014-10-10 21:18:50 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2014-10-11 03:50:21 +0200
commite6a7960b8f3c838c66ef8cdaed5faf11a8d1ab26 (patch)
tree95cc2910ca9066cc1eb3ab91ea3bfed90e4c0569 /OpenKeychain/src/main/java
parentfe981e54989c47ae252a4dfdc2aa41aab295cc7e (diff)
downloadopen-keychain-e6a7960b8f3c838c66ef8cdaed5faf11a8d1ab26.tar.gz
open-keychain-e6a7960b8f3c838c66ef8cdaed5faf11a8d1ab26.tar.bz2
open-keychain-e6a7960b8f3c838c66ef8cdaed5faf11a8d1ab26.zip
move delete into operation class
Diffstat (limited to 'OpenKeychain/src/main/java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/DeleteOperation.java85
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/DeleteResult.java57
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java11
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java39
4 files changed, 159 insertions, 33 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/DeleteOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/DeleteOperation.java
new file mode 100644
index 000000000..00d50aa9b
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/DeleteOperation.java
@@ -0,0 +1,85 @@
+package org.sufficientlysecure.keychain.operations;
+
+import android.content.Context;
+
+import org.sufficientlysecure.keychain.operations.results.ConsolidateResult;
+import org.sufficientlysecure.keychain.operations.results.DeleteResult;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
+import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
+import org.sufficientlysecure.keychain.pgp.Progressable;
+import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
+import org.sufficientlysecure.keychain.provider.ProviderHelper;
+import org.sufficientlysecure.keychain.service.ContactSyncAdapterService;
+import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
+
+public class DeleteOperation extends BaseOperation {
+
+ public DeleteOperation(Context context, ProviderHelper providerHelper, Progressable progressable) {
+ super(context, providerHelper, progressable);
+ }
+
+ public DeleteResult execute(long[] masterKeyIds, boolean isSecret) {
+
+ OperationLog log = new OperationLog();
+
+ if (masterKeyIds.length == 0) {
+ log.add(LogType.MSG_DEL_ERROR_EMPTY, 0);
+ return new DeleteResult(DeleteResult.RESULT_ERROR, log, 0, 0);
+ }
+
+ if (isSecret && masterKeyIds.length > 1) {
+ log.add(LogType.MSG_DEL_ERROR_MULTI_SECRET, 0);
+ return new DeleteResult(DeleteResult.RESULT_ERROR, log, 0, 0);
+ }
+
+ log.add(LogType.MSG_DEL, 0, masterKeyIds.length);
+
+ boolean cancelled = false;
+ int success = 0, fail = 0;
+ for (long masterKeyId : masterKeyIds) {
+ if (checkCancelled()) {
+ cancelled = true;
+ break;
+ }
+ int count = mProviderHelper.getContentResolver().delete(
+ KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null
+ );
+ if (count > 0) {
+ log.add(LogType.MSG_DEL_KEY, 1, KeyFormattingUtils.beautifyKeyId(masterKeyId));
+ success += 1;
+ } else {
+ log.add(LogType.MSG_DEL_KEY_FAIL, 1, KeyFormattingUtils.beautifyKeyId(masterKeyId));
+ fail += 1;
+ }
+ }
+
+ if (isSecret && success > 0) {
+ log.add(LogType.MSG_DEL_CONSOLIDATE, 1);
+ ConsolidateResult sub = mProviderHelper.consolidateDatabaseStep1(mProgressable);
+ log.add(sub, 2);
+ }
+
+ int result = DeleteResult.RESULT_OK;
+ if (success > 0) {
+ // make sure new data is synced into contacts
+ ContactSyncAdapterService.requestSync();
+
+ log.add(LogType.MSG_DEL_OK, 0, success);
+ }
+ if (fail > 0) {
+ log.add(LogType.MSG_DEL_FAIL, 0, fail);
+ result |= DeleteResult.RESULT_WARNINGS;
+ if (success == 0) {
+ result |= DeleteResult.RESULT_ERROR;
+ }
+ }
+ if (cancelled) {
+ log.add(LogType.MSG_OPERATION_CANCELLED, 0);
+ result |= DeleteResult.RESULT_CANCELLED;
+ }
+
+ return new DeleteResult(result, log, success, fail);
+
+ }
+
+}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/DeleteResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/DeleteResult.java
new file mode 100644
index 000000000..1dce8f89e
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/DeleteResult.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
+ * Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.sufficientlysecure.keychain.operations.results;
+
+import android.os.Parcel;
+
+public class DeleteResult extends OperationResult {
+
+ final public int mOk, mFail;
+
+ public DeleteResult(int result, OperationLog log, int ok, int fail) {
+ super(result, log);
+ mOk = ok;
+ mFail = fail;
+ }
+
+ /** Construct from a parcel - trivial because we have no extra data. */
+ public DeleteResult(Parcel source) {
+ super(source);
+ mOk = source.readInt();
+ mFail = source.readInt();
+ }
+
+ @Override
+ public void writeToParcel(Parcel dest, int flags) {
+ super.writeToParcel(dest, flags);
+ dest.writeInt(mOk);
+ dest.writeInt(mFail);
+ }
+
+ public static Creator<DeleteResult> CREATOR = new Creator<DeleteResult>() {
+ public DeleteResult createFromParcel(final Parcel source) {
+ return new DeleteResult(source);
+ }
+
+ public DeleteResult[] newArray(final int size) {
+ return new DeleteResult[size];
+ }
+ };
+
+}
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java
index 45470ef20..dc47942fc 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java
@@ -593,7 +593,16 @@ public abstract class OperationResult implements Parcelable {
MSG_GET_QUERY_TOO_SHORT(LogLevel.ERROR, R.string.msg_download_query_too_short),
MSG_GET_TOO_MANY_RESPONSES(LogLevel.ERROR, R.string.msg_download_too_many_responses),
MSG_GET_QUERY_TOO_SHORT_OR_TOO_MANY_RESPONSES(LogLevel.ERROR, R.string.msg_download_query_too_short_or_too_many_responses),
- MSG_GET_QUERY_FAILED(LogLevel.ERROR, R.string.msg_download_query_failed)
+ MSG_GET_QUERY_FAILED(LogLevel.ERROR, R.string.msg_download_query_failed),
+
+ MSG_DEL_ERROR_EMPTY (LogLevel.ERROR, R.string.msg_del_error_empty),
+ MSG_DEL_ERROR_MULTI_SECRET (LogLevel.DEBUG, R.string.msg_del_error_multi_secret),
+ MSG_DEL (LogLevel.START, R.plurals.msg_del),
+ MSG_DEL_KEY (LogLevel.DEBUG, R.string.msg_del_key),
+ MSG_DEL_KEY_FAIL (LogLevel.WARN, R.string.msg_del_key_fail),
+ MSG_DEL_CONSOLIDATE (LogLevel.DEBUG, R.string.msg_del_consolidate),
+ MSG_DEL_OK (LogLevel.OK, R.plurals.msg_del_ok),
+ MSG_DEL_FAIL (LogLevel.WARN, R.plurals.msg_del_fail),
;
public final int mMsgId;
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 f2b96d82c..1bbeaf936 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,9 @@ import android.os.RemoteException;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
+import org.sufficientlysecure.keychain.operations.DeleteOperation;
import org.sufficientlysecure.keychain.operations.PgpCertifyOperation;
+import org.sufficientlysecure.keychain.operations.results.DeleteResult;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException;
import org.sufficientlysecure.keychain.operations.results.CertifyResult;
@@ -362,40 +364,13 @@ public class KeychainIntentService extends IntentService implements Progressable
} else if (ACTION_DELETE.equals(action)) {
- try {
-
- long[] masterKeyIds = data.getLongArray(DELETE_KEY_LIST);
- boolean isSecret = data.getBoolean(DELETE_IS_SECRET);
-
- if (masterKeyIds.length == 0) {
- throw new PgpGeneralException("List of keys to delete is empty");
- }
-
- if (isSecret && masterKeyIds.length > 1) {
- throw new PgpGeneralException("Secret keys can only be deleted individually!");
- }
-
- boolean success = false;
- for (long masterKeyId : masterKeyIds) {
- int count = getContentResolver().delete(
- KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null
- );
- success |= count > 0;
- }
-
- if (isSecret && success) {
- new ProviderHelper(this).consolidateDatabaseStep1(this);
- }
+ long[] masterKeyIds = data.getLongArray(DELETE_KEY_LIST);
+ boolean isSecret = data.getBoolean(DELETE_IS_SECRET);
- if (success) {
- // make sure new data is synced into contacts
- ContactSyncAdapterService.requestSync();
+ DeleteOperation op = new DeleteOperation(this, new ProviderHelper(this), this);
+ DeleteResult result = op.execute(masterKeyIds, isSecret);
- sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY);
- }
- } catch (Exception e) {
- sendErrorToHandler(e);
- }
+ sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result);
} else if (ACTION_DELETE_FILE_SECURELY.equals(action)) {