use autovalue for InputDataParcel and SignEncryptParcel

This commit is contained in:
Vincent Breitmoser
2017-05-23 15:04:04 +02:00
parent 040a5a2006
commit 9d485dfe9f
8 changed files with 79 additions and 146 deletions

View File

@@ -86,8 +86,7 @@ public class BenchmarkOperation extends BaseOperation<BenchmarkInputParcel> {
PgpSignEncryptData.Builder data = PgpSignEncryptData.builder(); PgpSignEncryptData.Builder data = PgpSignEncryptData.builder();
data.setSymmetricPassphrase(passphrase); data.setSymmetricPassphrase(passphrase);
data.setSymmetricEncryptionAlgorithm(OpenKeychainSymmetricKeyAlgorithmTags.AES_128); data.setSymmetricEncryptionAlgorithm(OpenKeychainSymmetricKeyAlgorithmTags.AES_128);
SignEncryptParcel input = new SignEncryptParcel(data.build()); SignEncryptParcel input = SignEncryptParcel.createSignEncryptParcel(data.build(), buf);
input.setBytes(buf);
encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); encryptResult = op.execute(input, CryptoInputParcel.createCryptoInputParcel());
log.add(encryptResult, 1); log.add(encryptResult, 1);
log.add(LogType.MSG_BENCH_ENC_TIME, 2, log.add(LogType.MSG_BENCH_ENC_TIME, 2,

View File

@@ -26,7 +26,6 @@ import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog; import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.operations.results.PgpSignEncryptResult; import org.sufficientlysecure.keychain.operations.results.PgpSignEncryptResult;
@@ -36,13 +35,11 @@ import org.sufficientlysecure.keychain.pgp.PgpSignEncryptInputParcel;
import org.sufficientlysecure.keychain.pgp.PgpSignEncryptOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncryptOperation;
import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.pgp.SignEncryptParcel; import org.sufficientlysecure.keychain.pgp.SignEncryptParcel;
import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.KeyRepository; import org.sufficientlysecure.keychain.provider.KeyRepository;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel; import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.RequiredInputType; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.RequiredInputType;
import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.SecurityTokenSignOperationsBuilder; import org.sufficientlysecure.keychain.service.input.RequiredInputParcel.SecurityTokenSignOperationsBuilder;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ProgressScaler; import org.sufficientlysecure.keychain.util.ProgressScaler;
@@ -76,8 +73,6 @@ public class SignEncryptOperation extends BaseOperation<SignEncryptParcel> {
SecurityTokenSignOperationsBuilder pendingInputBuilder = null; SecurityTokenSignOperationsBuilder pendingInputBuilder = null;
PgpSignEncryptData data = input.getData();
do { do {
if (checkCancelled()) { if (checkCancelled()) {
log.add(LogType.MSG_OPERATION_CANCELLED, 0); log.add(LogType.MSG_OPERATION_CANCELLED, 0);
@@ -86,7 +81,7 @@ public class SignEncryptOperation extends BaseOperation<SignEncryptParcel> {
PgpSignEncryptOperation op = new PgpSignEncryptOperation(mContext, mKeyRepository, PgpSignEncryptOperation op = new PgpSignEncryptOperation(mContext, mKeyRepository,
new ProgressScaler(mProgressable, 100 * count / total, 100 * ++count / total, 100), mCancelled); new ProgressScaler(mProgressable, 100 * count / total, 100 * ++count / total, 100), mCancelled);
PgpSignEncryptInputParcel inputParcel = new PgpSignEncryptInputParcel(input.getData()); PgpSignEncryptInputParcel inputParcel = new PgpSignEncryptInputParcel(input.getSignEncryptData());
if (inputBytes != null) { if (inputBytes != null) {
inputParcel.setInputBytes(inputBytes); inputParcel.setInputBytes(inputBytes);
} else { } else {
@@ -106,7 +101,7 @@ public class SignEncryptOperation extends BaseOperation<SignEncryptParcel> {
} }
if (pendingInputBuilder == null) { if (pendingInputBuilder == null) {
pendingInputBuilder = new SecurityTokenSignOperationsBuilder(requiredInput.mSignatureTime, pendingInputBuilder = new SecurityTokenSignOperationsBuilder(requiredInput.mSignatureTime,
data.getSignatureMasterKeyId(), data.getSignatureSubKeyId()); requiredInput.getMasterKeyId(), requiredInput.getSubKeyId());
} }
pendingInputBuilder.addAll(requiredInput); pendingInputBuilder.addAll(requiredInput);
} else if (!result.success()) { } else if (!result.success()) {

View File

@@ -19,14 +19,17 @@
package org.sufficientlysecure.keychain.pgp; package org.sufficientlysecure.keychain.pgp;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import com.google.auto.value.AutoValue;
/** /**
* This parcel stores the input of one or more PgpSignEncrypt operations. * This parcel stores the input of one or more PgpSignEncrypt operations.
* All operations will use the same general parameters, differing only in * All operations will use the same general parameters, differing only in
@@ -39,83 +42,65 @@ import java.util.List;
* - Once the output uris are empty, there must be exactly one input (uri xor bytes) * - Once the output uris are empty, there must be exactly one input (uri xor bytes)
* left, which will be returned in a byte array as part of the result parcel. * left, which will be returned in a byte array as part of the result parcel.
*/ */
public class SignEncryptParcel implements Parcelable { @AutoValue
public abstract class SignEncryptParcel implements Parcelable {
public abstract PgpSignEncryptData getSignEncryptData();
public abstract List<Uri> getInputUris();
public abstract List<Uri> getOutputUris();
@SuppressWarnings("mutable")
@Nullable
public abstract byte[] getBytes();
private PgpSignEncryptData data;
public ArrayList<Uri> mInputUris = new ArrayList<>();
public ArrayList<Uri> mOutputUris = new ArrayList<>();
public byte[] mBytes;
public SignEncryptParcel(PgpSignEncryptData data) {
this.data = data;
}
public SignEncryptParcel(Parcel src) {
mInputUris = src.createTypedArrayList(Uri.CREATOR);
mOutputUris = src.createTypedArrayList(Uri.CREATOR);
mBytes = src.createByteArray();
data = src.readParcelable(getClass().getClassLoader());
}
public boolean isIncomplete() { public boolean isIncomplete() {
return mInputUris.size() > mOutputUris.size(); List<Uri> inputUris = getInputUris();
List<Uri> outputUris = getOutputUris();
if (inputUris == null || outputUris == null) {
throw new IllegalStateException("Invalid operation for bytes-backed SignEncryptParcel!");
}
return inputUris.size() > outputUris.size();
} }
public byte[] getBytes() {
return mBytes; public static SignEncryptParcel createSignEncryptParcel(PgpSignEncryptData signEncryptData, byte[] bytes) {
// noinspection unchecked, it's ok for the empty list
return new AutoValue_SignEncryptParcel(signEncryptData, Collections.EMPTY_LIST, Collections.EMPTY_LIST, bytes);
} }
public void setBytes(byte[] bytes) { public static Builder builder(SignEncryptParcel signEncryptParcel) {
mBytes = bytes; return new Builder(signEncryptParcel.getSignEncryptData())
.addInputUris(signEncryptParcel.getInputUris())
.addOutputUris(signEncryptParcel.getOutputUris());
} }
public List<Uri> getInputUris() { public static Builder builder(PgpSignEncryptData signEncryptData) {
return Collections.unmodifiableList(mInputUris); return new Builder(signEncryptData);
} }
public void addInputUris(Collection<Uri> inputUris) {
mInputUris.addAll(inputUris);
}
public List<Uri> getOutputUris() { public static class Builder {
return Collections.unmodifiableList(mOutputUris); private final PgpSignEncryptData signEncryptData;
} private ArrayList<Uri> inputUris = new ArrayList<>();
private ArrayList<Uri> outputUris = new ArrayList<>();
public void addOutputUris(ArrayList<Uri> outputUris) { private Builder(PgpSignEncryptData signEncryptData) {
mOutputUris.addAll(outputUris); this.signEncryptData = signEncryptData;
}
public void setData(PgpSignEncryptData data) {
this.data = data;
}
public PgpSignEncryptData getData() {
return data;
}
@Override
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(mInputUris);
dest.writeTypedList(mOutputUris);
dest.writeByteArray(mBytes);
dest.writeParcelable(data, 0);
}
public static final Creator<SignEncryptParcel> CREATOR = new Creator<SignEncryptParcel>() {
public SignEncryptParcel createFromParcel(final Parcel source) {
return new SignEncryptParcel(source);
} }
public SignEncryptParcel[] newArray(final int size) { public SignEncryptParcel build() {
return new SignEncryptParcel[size]; return new AutoValue_SignEncryptParcel(signEncryptData,
Collections.unmodifiableList(inputUris),
Collections.unmodifiableList(outputUris),
null);
} }
};
public Builder addOutputUris(Collection<Uri> outputUris) {
this.outputUris.addAll(outputUris);
return this;
}
public Builder addInputUris(Collection<Uri> inputUris) {
this.inputUris.addAll(inputUris);
return this;
}
}
} }

View File

@@ -17,65 +17,24 @@
package org.sufficientlysecure.keychain.service; package org.sufficientlysecure.keychain.service;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.net.Uri;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
public class InputDataParcel implements Parcelable { @AutoValue
public abstract class InputDataParcel implements Parcelable {
public abstract Uri getInputUri();
@Nullable
public abstract PgpDecryptVerifyInputParcel getDecryptInput();
public abstract boolean getMimeDecode(); // TODO static value - ditch this?
private Uri mInputUri; public static InputDataParcel createInputDataParcel(Uri inputUri, PgpDecryptVerifyInputParcel decryptInput) {
return new AutoValue_InputDataParcel(inputUri, decryptInput, true);
private PgpDecryptVerifyInputParcel mDecryptInput;
private boolean mMimeDecode = true; // TODO default to false
public InputDataParcel(Uri inputUri, PgpDecryptVerifyInputParcel decryptInput) {
mInputUri = inputUri;
mDecryptInput = decryptInput;
} }
InputDataParcel(Parcel source) {
// we do all of those here, so the PgpSignEncryptInput class doesn't have to be parcelable
mInputUri = source.readParcelable(getClass().getClassLoader());
mDecryptInput = source.readParcelable(getClass().getClassLoader());
mMimeDecode = source.readInt() != 0;
}
public Uri getInputUri() {
return mInputUri;
}
public PgpDecryptVerifyInputParcel getDecryptInput() {
return mDecryptInput;
}
public boolean getMimeDecode() {
return mMimeDecode;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(mInputUri, 0);
dest.writeParcelable(mDecryptInput, 0);
dest.writeInt(mMimeDecode ? 1 : 0);
}
public static final Creator<InputDataParcel> CREATOR = new Creator<InputDataParcel>() {
public InputDataParcel createFromParcel(final Parcel source) {
return new InputDataParcel(source);
}
public InputDataParcel[] newArray(final int size) {
return new InputDataParcel[size];
}
};
} }

View File

@@ -639,7 +639,7 @@ public class DecryptListFragment
PgpDecryptVerifyInputParcel decryptInput = new PgpDecryptVerifyInputParcel() PgpDecryptVerifyInputParcel decryptInput = new PgpDecryptVerifyInputParcel()
.setAllowSymmetricDecryption(true); .setAllowSymmetricDecryption(true);
return new InputDataParcel(mCurrentInputUri, decryptInput); return InputDataParcel.createInputDataParcel(mCurrentInputUri, decryptInput);
} }

View File

@@ -577,12 +577,10 @@ public class EncryptFilesFragment
} }
public SignEncryptParcel createOperationInput() { public SignEncryptParcel createOperationInput() {
SignEncryptParcel actionsParcel = getCachedActionsParcel(); SignEncryptParcel actionsParcel = getCachedActionsParcel();
// we have three cases here: nothing cached, cached except output, fully cached // we have three cases here: nothing cached, cached except output, fully cached
if (actionsParcel == null) { if (actionsParcel == null) {
// clear output uris for now, they will be created by prepareOutputStreams later // clear output uris for now, they will be created by prepareOutputStreams later
mOutputUris = null; mOutputUris = null;
@@ -593,7 +591,6 @@ public class EncryptFilesFragment
} }
cacheActionsParcel(actionsParcel); cacheActionsParcel(actionsParcel);
} }
// if it's incomplete, prepare output streams // if it's incomplete, prepare output streams
@@ -606,9 +603,10 @@ public class EncryptFilesFragment
} }
} }
actionsParcel.addOutputUris(mOutputUris); actionsParcel = SignEncryptParcel.builder(actionsParcel)
.addOutputUris(mOutputUris)
.build();
cacheActionsParcel(actionsParcel); cacheActionsParcel(actionsParcel);
} }
return actionsParcel; return actionsParcel;
@@ -675,10 +673,9 @@ public class EncryptFilesFragment
} }
SignEncryptParcel parcel = new SignEncryptParcel(data.build()); SignEncryptParcel.Builder builder = SignEncryptParcel.builder(data.build());
parcel.addInputUris(mFilesAdapter.getAsArrayList()); builder.addInputUris(mFilesAdapter.getAsArrayList());
return builder.build();
return parcel;
} }
private Intent createSendIntent() { private Intent createSendIntent() {

View File

@@ -17,6 +17,11 @@
package org.sufficientlysecure.keychain.ui; package org.sufficientlysecure.keychain.ui;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import android.app.Activity; import android.app.Activity;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
@@ -49,10 +54,6 @@ import org.sufficientlysecure.keychain.ui.util.Notify.Style;
import org.sufficientlysecure.keychain.util.Passphrase; import org.sufficientlysecure.keychain.util.Passphrase;
import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.Preferences;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class EncryptTextFragment public class EncryptTextFragment
extends CachingCryptoOperationFragment<SignEncryptParcel, SignEncryptResult> { extends CachingCryptoOperationFragment<SignEncryptParcel, SignEncryptResult> {
@@ -286,10 +287,7 @@ public class EncryptTextFragment
data.setSymmetricPassphrase(passphrase); data.setSymmetricPassphrase(passphrase);
} }
SignEncryptParcel parcel = new SignEncryptParcel(data.build()); return SignEncryptParcel.createSignEncryptParcel(data.build(), mMessage.getBytes());
parcel.setBytes(mMessage.getBytes());
return parcel;
} }
private void copyToClipboard(SignEncryptResult result) { private void copyToClipboard(SignEncryptResult result) {

View File

@@ -128,7 +128,7 @@ public class InputDataOperationTest {
InputDataOperation op = new InputDataOperation(spyApplication, InputDataOperation op = new InputDataOperation(spyApplication,
KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null); KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null);
InputDataParcel input = new InputDataParcel(fakeInputUri, null); InputDataParcel input = InputDataParcel.createInputDataParcel(fakeInputUri, null);
InputDataResult result = op.execute(input, CryptoInputParcel.createCryptoInputParcel()); InputDataResult result = op.execute(input, CryptoInputParcel.createCryptoInputParcel());
@@ -308,7 +308,7 @@ public class InputDataOperationTest {
InputDataOperation op = new InputDataOperation(spyApplication, InputDataOperation op = new InputDataOperation(spyApplication,
KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null); KeyWritableRepository.createDatabaseReadWriteInteractor(RuntimeEnvironment.application), null);
InputDataParcel input = new InputDataParcel(FAKE_CONTENT_INPUT_URI_1, null); InputDataParcel input = InputDataParcel.createInputDataParcel(FAKE_CONTENT_INPUT_URI_1, null);
return op.execute(input, CryptoInputParcel.createCryptoInputParcel()); return op.execute(input, CryptoInputParcel.createCryptoInputParcel());
} }