OpenPgpSignatureResult.java

  1/*
  2 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package org.openintents.openpgp;
 18
 19import android.os.Parcel;
 20import android.os.Parcelable;
 21
 22import org.openintents.openpgp.util.OpenPgpUtils;
 23
 24import java.util.ArrayList;
 25import java.util.Locale;
 26
 27/**
 28 * Parcelable versioning has been copied from Dashclock Widget
 29 * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java
 30 */
 31public class OpenPgpSignatureResult implements Parcelable {
 32    /**
 33     * Since there might be a case where new versions of the client using the library getting
 34     * old versions of the protocol (and thus old versions of this class), we need a versioning
 35     * system for the parcels sent between the clients and the providers.
 36     */
 37    public static final int PARCELABLE_VERSION = 2;
 38
 39    // generic error on signature verification
 40    public static final int SIGNATURE_ERROR = 0;
 41    // successfully verified signature, with certified key
 42    public static final int SIGNATURE_SUCCESS_CERTIFIED = 1;
 43    // no key was found for this signature verification
 44    public static final int SIGNATURE_KEY_MISSING = 2;
 45    // successfully verified signature, but with uncertified key
 46    public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3;
 47    // key has been revoked
 48    public static final int SIGNATURE_KEY_REVOKED = 4;
 49    // key is expired
 50    public static final int SIGNATURE_KEY_EXPIRED = 5;
 51
 52    int status;
 53    boolean signatureOnly;
 54    String primaryUserId;
 55    ArrayList<String> userIds;
 56    long keyId;
 57
 58    public int getStatus() {
 59        return status;
 60    }
 61
 62    public void setStatus(int status) {
 63        this.status = status;
 64    }
 65
 66    public boolean isSignatureOnly() {
 67        return signatureOnly;
 68    }
 69
 70    public void setSignatureOnly(boolean signatureOnly) {
 71        this.signatureOnly = signatureOnly;
 72    }
 73
 74    public String getPrimaryUserId() {
 75        return primaryUserId;
 76    }
 77
 78    public void setPrimaryUserId(String primaryUserId) {
 79        this.primaryUserId = primaryUserId;
 80    }
 81
 82    public ArrayList<String> getUserIds() {
 83        return userIds;
 84    }
 85
 86    public void setUserIds(ArrayList<String> userIds) {
 87        this.userIds = userIds;
 88    }
 89
 90    public long getKeyId() {
 91        return keyId;
 92    }
 93
 94    public void setKeyId(long keyId) {
 95        this.keyId = keyId;
 96    }
 97
 98    public OpenPgpSignatureResult() {
 99
100    }
101
102    public OpenPgpSignatureResult(int signatureStatus, String signatureUserId,
103                                  boolean signatureOnly, long keyId, ArrayList<String> userIds) {
104        this.status = signatureStatus;
105        this.signatureOnly = signatureOnly;
106        this.primaryUserId = signatureUserId;
107        this.keyId = keyId;
108        this.userIds = userIds;
109    }
110
111    public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
112        this.status = b.status;
113        this.primaryUserId = b.primaryUserId;
114        this.signatureOnly = b.signatureOnly;
115        this.keyId = b.keyId;
116        this.userIds = b.userIds;
117    }
118
119    public int describeContents() {
120        return 0;
121    }
122
123    public void writeToParcel(Parcel dest, int flags) {
124        /**
125         * NOTE: When adding fields in the process of updating this API, make sure to bump
126         * {@link #PARCELABLE_VERSION}.
127         */
128        dest.writeInt(PARCELABLE_VERSION);
129        // Inject a placeholder that will store the parcel size from this point on
130        // (not including the size itself).
131        int sizePosition = dest.dataPosition();
132        dest.writeInt(0);
133        int startPosition = dest.dataPosition();
134        // version 1
135        dest.writeInt(status);
136        dest.writeByte((byte) (signatureOnly ? 1 : 0));
137        dest.writeString(primaryUserId);
138        dest.writeLong(keyId);
139        // version 2
140        dest.writeStringList(userIds);
141        // Go back and write the size
142        int parcelableSize = dest.dataPosition() - startPosition;
143        dest.setDataPosition(sizePosition);
144        dest.writeInt(parcelableSize);
145        dest.setDataPosition(startPosition + parcelableSize);
146    }
147
148    public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
149        public OpenPgpSignatureResult createFromParcel(final Parcel source) {
150            int parcelableVersion = source.readInt();
151            int parcelableSize = source.readInt();
152            int startPosition = source.dataPosition();
153
154            OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
155            vr.status = source.readInt();
156            vr.signatureOnly = source.readByte() == 1;
157            vr.primaryUserId = source.readString();
158            vr.keyId = source.readLong();
159            vr.userIds = new ArrayList<String>();
160            source.readStringList(vr.userIds);
161
162            // skip over all fields added in future versions of this parcel
163            source.setDataPosition(startPosition + parcelableSize);
164
165            return vr;
166        }
167
168        public OpenPgpSignatureResult[] newArray(final int size) {
169            return new OpenPgpSignatureResult[size];
170        }
171    };
172
173    @Override
174    public String toString() {
175        String out = "\nstatus: " + status;
176        out += "\nprimaryUserId: " + primaryUserId;
177        out += "\nuserIds: " + userIds;
178        out += "\nsignatureOnly: " + signatureOnly;
179        out += "\nkeyId: " + OpenPgpUtils.convertKeyIdToHex(keyId);
180        return out;
181    }
182
183}