OpenPgpError.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
 22/**
 23 * Parcelable versioning has been copied from Dashclock Widget
 24 * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java
 25 */
 26public class OpenPgpError implements Parcelable {
 27    /**
 28     * Since there might be a case where new versions of the client using the library getting
 29     * old versions of the protocol (and thus old versions of this class), we need a versioning
 30     * system for the parcels sent between the clients and the providers.
 31     */
 32    public static final int PARCELABLE_VERSION = 1;
 33
 34    // possible values for errorId
 35    public static final int CLIENT_SIDE_ERROR = -1;
 36    public static final int GENERIC_ERROR = 0;
 37    public static final int INCOMPATIBLE_API_VERSIONS = 1;
 38    public static final int NO_OR_WRONG_PASSPHRASE = 2;
 39    public static final int NO_USER_IDS = 3;
 40
 41    int errorId;
 42    String message;
 43
 44    public OpenPgpError() {
 45    }
 46
 47    public OpenPgpError(int errorId, String message) {
 48        this.errorId = errorId;
 49        this.message = message;
 50    }
 51
 52    public OpenPgpError(OpenPgpError b) {
 53        this.errorId = b.errorId;
 54        this.message = b.message;
 55    }
 56
 57    public int getErrorId() {
 58        return errorId;
 59    }
 60
 61    public void setErrorId(int errorId) {
 62        this.errorId = errorId;
 63    }
 64
 65    public String getMessage() {
 66        return message;
 67    }
 68
 69    public void setMessage(String message) {
 70        this.message = message;
 71    }
 72
 73    public int describeContents() {
 74        return 0;
 75    }
 76
 77    public void writeToParcel(Parcel dest, int flags) {
 78        /**
 79         * NOTE: When adding fields in the process of updating this API, make sure to bump
 80         * {@link #PARCELABLE_VERSION}.
 81         */
 82        dest.writeInt(PARCELABLE_VERSION);
 83        // Inject a placeholder that will store the parcel size from this point on
 84        // (not including the size itself).
 85        int sizePosition = dest.dataPosition();
 86        dest.writeInt(0);
 87        int startPosition = dest.dataPosition();
 88        // version 1
 89        dest.writeInt(errorId);
 90        dest.writeString(message);
 91        // Go back and write the size
 92        int parcelableSize = dest.dataPosition() - startPosition;
 93        dest.setDataPosition(sizePosition);
 94        dest.writeInt(parcelableSize);
 95        dest.setDataPosition(startPosition + parcelableSize);
 96    }
 97
 98    public static final Creator<OpenPgpError> CREATOR = new Creator<OpenPgpError>() {
 99        public OpenPgpError createFromParcel(final Parcel source) {
100            int parcelableVersion = source.readInt();
101            int parcelableSize = source.readInt();
102            int startPosition = source.dataPosition();
103
104            OpenPgpError error = new OpenPgpError();
105            error.errorId = source.readInt();
106            error.message = source.readString();
107
108            // skip over all fields added in future versions of this parcel
109            source.setDataPosition(startPosition + parcelableSize);
110
111            return error;
112        }
113
114        public OpenPgpError[] newArray(final int size) {
115            return new OpenPgpError[size];
116        }
117    };
118}