use autovalue for CertifyActionsParcel

This commit is contained in:
Vincent Breitmoser
2017-05-22 11:25:52 +02:00
parent 63774a0632
commit 53dcb4102d
9 changed files with 125 additions and 130 deletions

View File

@@ -18,99 +18,80 @@
package org.sufficientlysecure.keychain.service;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import android.os.Parcelable;
import android.support.annotation.CheckResult;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
import org.sufficientlysecure.keychain.keyimport.ParcelableHkpKeyserver;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
/**
* This class is a a transferable representation for a number of keyrings to
* be certified.
*/
public class CertifyActionsParcel implements Parcelable {
@AutoValue
public abstract class CertifyActionsParcel implements Parcelable {
public abstract long getMasterKeyId();
public abstract ArrayList<CertifyAction> getCertifyActions();
@Nullable
public abstract ParcelableHkpKeyserver getParcelableKeyServer();
// the master key id to certify with
final public long mMasterKeyId;
public CertifyLevel mLevel;
public ArrayList<CertifyAction> mCertifyActions = new ArrayList<>();
public ParcelableHkpKeyserver keyServerUri;
public CertifyActionsParcel(long masterKeyId) {
mMasterKeyId = masterKeyId;
mLevel = CertifyLevel.DEFAULT;
public static Builder builder(long masterKeyId) {
return new AutoValue_CertifyActionsParcel.Builder()
.setMasterKeyId(masterKeyId)
.setCertifyActions(new ArrayList<CertifyAction>());
}
public CertifyActionsParcel(Parcel source) {
mMasterKeyId = source.readLong();
// just like parcelables, this is meant for ad-hoc IPC only and is NOT portable!
mLevel = CertifyLevel.values()[source.readInt()];
keyServerUri = source.readParcelable(ParcelableHkpKeyserver.class.getClassLoader());
@AutoValue.Builder
public abstract static class Builder {
abstract Builder setMasterKeyId(long masterKeyId);
public abstract Builder setCertifyActions(ArrayList<CertifyAction> certifyActions);
public abstract Builder setParcelableKeyServer(ParcelableHkpKeyserver uri);
mCertifyActions = (ArrayList<CertifyAction>) source.readSerializable();
}
abstract ArrayList<CertifyAction> getCertifyActions();
public void add(CertifyAction action) {
mCertifyActions.add(action);
}
@Override
public void writeToParcel(Parcel destination, int flags) {
destination.writeLong(mMasterKeyId);
destination.writeInt(mLevel.ordinal());
destination.writeParcelable(keyServerUri, flags);
destination.writeSerializable(mCertifyActions);
}
public static final Creator<CertifyActionsParcel> CREATOR = new Creator<CertifyActionsParcel>() {
public CertifyActionsParcel createFromParcel(final Parcel source) {
return new CertifyActionsParcel(source);
public void addAction(CertifyAction action) {
getCertifyActions().add(action);
}
public void addActions(Collection<CertifyAction> certifyActions) {
getCertifyActions().addAll(certifyActions);
}
public CertifyActionsParcel[] newArray(final int size) {
return new CertifyActionsParcel[size];
public abstract CertifyActionsParcel build();
}
@AutoValue
public abstract static class CertifyAction implements Parcelable {
public abstract long getMasterKeyId();
@Nullable
public abstract ArrayList<String> getUserIds();
@Nullable
public abstract ArrayList<WrappedUserAttribute> getUserAttributes();
public static CertifyAction createForUserIds(long masterKeyId, List<String> userIds) {
return new AutoValue_CertifyActionsParcel_CertifyAction(masterKeyId, new ArrayList<>(userIds), null);
}
};
// TODO make this parcelable
public static class CertifyAction implements Serializable {
final public long mMasterKeyId;
public static CertifyAction createForUserAttributes(long masterKeyId, List<WrappedUserAttribute> attributes) {
return new AutoValue_CertifyActionsParcel_CertifyAction(masterKeyId, null, new ArrayList<>(attributes));
}
final public ArrayList<String> mUserIds;
final public ArrayList<WrappedUserAttribute> mUserAttributes;
@CheckResult
public CertifyAction withAddedUserIds(ArrayList<String> addedUserIds) {
if (getUserAttributes() != null) {
throw new IllegalStateException("Can't add user ids to user attribute certification parcel!");
}
ArrayList<String> prevUserIds = getUserIds();
if (prevUserIds == null) {
throw new IllegalStateException("Can't add user ids to user attribute certification parcel!");
}
public CertifyAction(long masterKeyId, List<String> userIds, List<WrappedUserAttribute> attributes) {
mMasterKeyId = masterKeyId;
mUserIds = userIds == null ? null : new ArrayList<>(userIds);
mUserAttributes = attributes == null ? null : new ArrayList<>(attributes);
ArrayList<String> userIds = new ArrayList<>(prevUserIds);
userIds.addAll(addedUserIds);
return new AutoValue_CertifyActionsParcel_CertifyAction(getMasterKeyId(), userIds, null);
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public String toString() {
String out = "mMasterKeyId: " + mMasterKeyId + "\n";
out += "mLevel: " + mLevel + "\n";
out += "mCertifyActions: " + mCertifyActions + "\n";
return out;
}
// All supported algorithms
public enum CertifyLevel {
DEFAULT, NONE, CASUAL, POSITIVE
}
}