|
8 | 8 | */ |
9 | 9 | package com.parse; |
10 | 10 |
|
| 11 | +import android.os.Parcel; |
| 12 | +import android.os.Parcelable; |
| 13 | + |
11 | 14 | import org.json.JSONException; |
12 | 15 | import org.json.JSONObject; |
13 | 16 |
|
|
40 | 43 | * object.save(); |
41 | 44 | * </pre> |
42 | 45 | */ |
43 | | -public class ParseFile { |
| 46 | +public class ParseFile implements Parcelable { |
44 | 47 |
|
45 | 48 | /* package for tests */ static ParseFileController getFileController() { |
46 | 49 | return ParseCorePlugins.getInstance().getFileController(); |
@@ -221,6 +224,33 @@ public ParseFile(byte[] data, String contentType) { |
221 | 224 | this(null, data, contentType); |
222 | 225 | } |
223 | 226 |
|
| 227 | + /** |
| 228 | + * Creates a new file instance from a {@link Parcel} source. This is used when unparceling |
| 229 | + * a non-dirty ParseFile. Subclasses that need Parcelable behavior should provide their own |
| 230 | + * {@link android.os.Parcelable.Creator} and override this constructor. |
| 231 | + * |
| 232 | + * @param source |
| 233 | + * the source Parcel |
| 234 | + */ |
| 235 | + protected ParseFile(Parcel source) { |
| 236 | + this(source, ParseParcelDecoder.get()); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Creates a new file instance from a {@link Parcel} using the given {@link ParseParcelDecoder}. |
| 241 | + * The decoder is currently unused, but it might be in the future, plus this is the pattern we |
| 242 | + * are using in parcelable classes. |
| 243 | + * @param source the parcel |
| 244 | + * @param decoder the decoder |
| 245 | + */ |
| 246 | + ParseFile(Parcel source, ParseParcelDecoder decoder) { |
| 247 | + this(new State.Builder() |
| 248 | + .url(source.readString()) |
| 249 | + .name(source.readString()) |
| 250 | + .mimeType(source.readByte() == 1 ? source.readString() : null) |
| 251 | + .build()); |
| 252 | + } |
| 253 | + |
224 | 254 | /* package for tests */ ParseFile(State state) { |
225 | 255 | this.state = state; |
226 | 256 | } |
@@ -705,4 +735,39 @@ public void cancel() { |
705 | 735 |
|
706 | 736 | return json; |
707 | 737 | } |
| 738 | + |
| 739 | + @Override |
| 740 | + public int describeContents() { |
| 741 | + return 0; |
| 742 | + } |
| 743 | + |
| 744 | + @Override |
| 745 | + public void writeToParcel(Parcel dest, int flags) { |
| 746 | + writeToParcel(dest, ParseParcelEncoder.get()); |
| 747 | + } |
| 748 | + |
| 749 | + void writeToParcel(Parcel dest, ParseParcelEncoder encoder) { |
| 750 | + if (isDirty()) { |
| 751 | + throw new RuntimeException("Unable to parcel an unsaved ParseFile."); |
| 752 | + } |
| 753 | + dest.writeString(getUrl()); // Not null |
| 754 | + dest.writeString(getName()); // Not null |
| 755 | + String type = state.mimeType(); // Nullable |
| 756 | + dest.writeByte(type != null ? (byte) 1 : 0); |
| 757 | + if (type != null) { |
| 758 | + dest.writeString(type); |
| 759 | + } |
| 760 | + } |
| 761 | + |
| 762 | + public final static Creator<ParseFile> CREATOR = new Creator<ParseFile>() { |
| 763 | + @Override |
| 764 | + public ParseFile createFromParcel(Parcel source) { |
| 765 | + return new ParseFile(source); |
| 766 | + } |
| 767 | + |
| 768 | + @Override |
| 769 | + public ParseFile[] newArray(int size) { |
| 770 | + return new ParseFile[size]; |
| 771 | + } |
| 772 | + }; |
708 | 773 | } |
0 commit comments