OpenPgpDecryptMetadata.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 OpenPgpDecryptMetadata 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    String filename;
 35    long modificationTime;
 36    int format;
 37    long originalSize;
 38
 39    public String getFilename() {
 40        return filename;
 41    }
 42
 43    public long getModificationTime() {
 44        return modificationTime;
 45    }
 46
 47    public long getOriginalSize() {
 48        return originalSize;
 49    }
 50
 51    public int getFormat() {
 52        return format;
 53    }
 54
 55    public OpenPgpDecryptMetadata() {
 56    }
 57
 58    public OpenPgpDecryptMetadata(String filename, long modificationTime,
 59                                  int format, long originalSize) {
 60        this.filename = filename;
 61        this.modificationTime = modificationTime;
 62        this.format = format;
 63        this.originalSize = originalSize;
 64    }
 65
 66    public OpenPgpDecryptMetadata(OpenPgpDecryptMetadata b) {
 67        this.filename = b.filename;
 68        this.modificationTime = b.modificationTime;
 69        this.format = b.format;
 70        this.originalSize = b.originalSize;
 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.writeString(filename);
 90        dest.writeLong(modificationTime);
 91        dest.writeInt(format);
 92        dest.writeLong(originalSize);
 93        // Go back and write the size
 94        int parcelableSize = dest.dataPosition() - startPosition;
 95        dest.setDataPosition(sizePosition);
 96        dest.writeInt(parcelableSize);
 97        dest.setDataPosition(startPosition + parcelableSize);
 98    }
 99
100    public static final Creator<OpenPgpDecryptMetadata> CREATOR = new Creator<OpenPgpDecryptMetadata>() {
101        public OpenPgpDecryptMetadata createFromParcel(final Parcel source) {
102            int parcelableVersion = source.readInt();
103            int parcelableSize = source.readInt();
104            int startPosition = source.dataPosition();
105
106            OpenPgpDecryptMetadata vr = new OpenPgpDecryptMetadata();
107            vr.filename = source.readString();
108            vr.modificationTime = source.readLong();
109            vr.format = source.readInt();
110            vr.originalSize = source.readLong();
111
112            // skip over all fields added in future versions of this parcel
113            source.setDataPosition(startPosition + parcelableSize);
114
115            return vr;
116        }
117
118        public OpenPgpDecryptMetadata[] newArray(final int size) {
119            return new OpenPgpDecryptMetadata[size];
120        }
121    };
122
123    @Override
124    public String toString() {
125        String out = new String();
126        out += "\nfilename: " + filename;
127        out += "\nmodificationTime: " + modificationTime;
128        out += "\nformat: " + format;
129        out += "\noriginalSize: " + originalSize;
130        return out;
131    }
132
133}