use autovalue for UploadKeyringParcel

This commit is contained in:
Vincent Breitmoser 2017-05-22 11:26:38 +02:00
parent 53dcb4102d
commit f1cf759e0a
6 changed files with 25 additions and 66 deletions

View file

@ -144,7 +144,7 @@ public class EditKeyOperation extends BaseReadWriteOperation<SaveKeyringParcel>
}
UploadKeyringParcel exportKeyringParcel =
new UploadKeyringParcel(saveParcel.getUploadKeyserver(), keyringBytes);
UploadKeyringParcel.createWithKeyringBytes(saveParcel.getUploadKeyserver(), keyringBytes);
UploadResult uploadResult = new UploadOperation(
mContext, mKeyRepository, new ProgressScaler(mProgressable, 60, 80, 100), mCancelled)

View file

@ -96,7 +96,7 @@ public class UploadOperation extends BaseOperation<UploadKeyringParcel> {
ParcelableHkpKeyserver hkpKeyserver;
{
hkpKeyserver = uploadInput.mKeyserver;
hkpKeyserver = uploadInput.getKeyserver();
log.add(LogType.MSG_UPLOAD_SERVER, 1, hkpKeyserver.toString());
}
@ -110,22 +110,15 @@ public class UploadOperation extends BaseOperation<UploadKeyringParcel> {
@Nullable
private CanonicalizedPublicKeyRing getPublicKeyringFromInput(OperationLog log, UploadKeyringParcel uploadInput) {
boolean hasMasterKeyId = uploadInput.mMasterKeyId != null;
boolean hasKeyringBytes = uploadInput.mUncachedKeyringBytes != null;
if (hasMasterKeyId == hasKeyringBytes) {
throw new IllegalArgumentException("either keyid xor bytes must be non-null for this method call!");
}
try {
if (hasMasterKeyId) {
log.add(LogType.MSG_UPLOAD_KEY, 0, KeyFormattingUtils.convertKeyIdToHex(uploadInput.mMasterKeyId));
return mKeyRepository.getCanonicalizedPublicKeyRing(uploadInput.mMasterKeyId);
Long masterKeyId = uploadInput.getMasterKeyId();
if (masterKeyId != null) {
log.add(LogType.MSG_UPLOAD_KEY, 0, KeyFormattingUtils.convertKeyIdToHex(masterKeyId));
return mKeyRepository.getCanonicalizedPublicKeyRing(masterKeyId);
}
CanonicalizedKeyRing canonicalizedRing =
UncachedKeyRing.decodeFromData(uploadInput.mUncachedKeyringBytes)
UncachedKeyRing.decodeFromData(uploadInput.getUncachedKeyringBytes())
.canonicalize(new OperationLog(), 0, true);
if (!CanonicalizedPublicKeyRing.class.isInstance(canonicalizedRing)) {
throw new IllegalArgumentException("keyring bytes must contain public key ring!");

View file

@ -20,62 +20,28 @@
package org.sufficientlysecure.keychain.service;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
import org.sufficientlysecure.keychain.keyimport.ParcelableHkpKeyserver;
@AutoValue
public abstract class UploadKeyringParcel implements Parcelable {
public abstract ParcelableHkpKeyserver getKeyserver();
@Nullable
public abstract Long getMasterKeyId();
@Nullable
public abstract byte[] getUncachedKeyringBytes();
public class UploadKeyringParcel implements Parcelable {
public ParcelableHkpKeyserver mKeyserver;
public final Long mMasterKeyId;
public final byte[] mUncachedKeyringBytes;
public UploadKeyringParcel(ParcelableHkpKeyserver keyserver, long masterKeyId) {
mKeyserver = keyserver;
mMasterKeyId = masterKeyId;
mUncachedKeyringBytes = null;
public static UploadKeyringParcel createWithKeyId(ParcelableHkpKeyserver keyserver, long masterKeyId) {
return new AutoValue_UploadKeyringParcel(keyserver, masterKeyId, null);
}
public UploadKeyringParcel(ParcelableHkpKeyserver keyserver, byte[] uncachedKeyringBytes) {
mKeyserver = keyserver;
mMasterKeyId = null;
mUncachedKeyringBytes = uncachedKeyringBytes;
public static UploadKeyringParcel createWithKeyringBytes(ParcelableHkpKeyserver keyserver,
@NonNull byte[] uncachedKeyringBytes) {
return new AutoValue_UploadKeyringParcel(keyserver, null, uncachedKeyringBytes);
}
protected UploadKeyringParcel(Parcel in) {
mKeyserver = in.readParcelable(ParcelableHkpKeyserver.class.getClassLoader());
mMasterKeyId = in.readInt() != 0 ? in.readLong() : null;
mUncachedKeyringBytes = in.createByteArray();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(mKeyserver, flags);
if (mMasterKeyId != null) {
dest.writeInt(1);
dest.writeLong(mMasterKeyId);
} else {
dest.writeInt(0);
}
dest.writeByteArray(mUncachedKeyringBytes);
}
public static final Creator<UploadKeyringParcel> CREATOR = new Creator<UploadKeyringParcel>() {
@Override
public UploadKeyringParcel createFromParcel(Parcel in) {
return new UploadKeyringParcel(in);
}
@Override
public UploadKeyringParcel[] newArray(int size) {
return new UploadKeyringParcel[size];
}
};
}

View file

@ -520,7 +520,7 @@ public class CreateKeyFinalFragment extends Fragment {
@Override
public UploadKeyringParcel createOperationInput() {
return new UploadKeyringParcel(keyserver, masterKeyId);
return UploadKeyringParcel.createWithKeyId(keyserver, masterKeyId);
}
@Override

View file

@ -395,7 +395,7 @@ public class EditIdentitiesFragment extends Fragment
@Override
public UploadKeyringParcel createOperationInput() {
return new UploadKeyringParcel(keyserver, masterKeyId);
return UploadKeyringParcel.createWithKeyId(keyserver, masterKeyId);
}
@Override

View file

@ -144,7 +144,7 @@ public class UploadKeyActivity extends BaseActivity
public UploadKeyringParcel createOperationInput() {
long[] masterKeyIds = getIntent().getLongArrayExtra(MultiUserIdsFragment.EXTRA_KEY_IDS);
return new UploadKeyringParcel(mKeyserver, masterKeyIds[0]);
return UploadKeyringParcel.createWithKeyId(mKeyserver, masterKeyIds[0]);
}
@Override