blob: b81719f3b115a6438755ea2f6bc401b5f90f3ed8 [file] [log] [blame]
Cheuksan Wangf9c50c42014-10-21 15:58:23 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
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 android.service.carriermessaging;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.ArrayList;
23import java.util.List;
24
25/**
26 * A parcelable list of PDUs representing contents of a possibly multi-part SMS.
27 */
28public final class MessagePdu implements Parcelable {
29 private static final int NULL_LENGTH = -1;
30
31 private final List<byte[]> mPduList;
32
33 /**
34 * @param pduList the list of message PDUs
35 */
36 public MessagePdu(List<byte[]> pduList) {
37 mPduList = pduList;
38 }
39
40 /**
41 * Returns the contents of a possibly multi-part SMS.
42 *
43 * @return the list of PDUs
44 */
45 public List<byte[]> getPdus() {
46 return mPduList;
47 }
48
49 @Override
50 public int describeContents() {
51 return 0;
52 }
53
54 /**
55 * Writes the PDU into a {@link Parcel}.
56 */
57 @Override
58 public void writeToParcel(Parcel dest, int flags) {
59 if (mPduList == null) {
60 dest.writeInt(NULL_LENGTH);
61 } else {
62 dest.writeInt(mPduList.size());
63 for (byte[] messagePdu : mPduList) {
64 dest.writeByteArray(messagePdu);
65 }
66 }
67 }
68
69 /**
70 * Constructs a {@link MessagePdu} from a {@link Parcel}.
71 */
72 public static final Parcelable.Creator<MessagePdu> CREATOR
73 = new Parcelable.Creator<MessagePdu>() {
74 @Override
75 public MessagePdu createFromParcel(Parcel source) {
76 int size = source.readInt();
77 List<byte[]> pduList;
78 if (size == NULL_LENGTH) {
79 pduList = null;
80 } else {
81 pduList = new ArrayList<>(size);
82 for (int i = 0; i < size; i++) {
83 pduList.add(source.createByteArray());
84 }
85 }
86 return new MessagePdu(pduList);
87 }
88
89 @Override
90 public MessagePdu[] newArray(int size) {
91 return new MessagePdu[size];
92 }
93 };
94}