MessagePdu.java revision b72eb97e4bca1e4fd68e79f9d04e9a6a15aebd21
1/*
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.carrier;
18
19import android.annotation.NonNull;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * A parcelable list of PDUs representing contents of a possibly multi-part SMS.
28 */
29public final class MessagePdu implements Parcelable {
30    private static final int NULL_LENGTH = -1;
31
32    private final List<byte[]> mPduList;
33
34    /**
35     * Constructs a MessagePdu with the list of message PDUs.
36     *
37     * @param pduList the list of message PDUs
38     */
39    public MessagePdu(@NonNull List<byte[]> pduList) {
40        if (pduList == null || pduList.contains(null)) {
41            throw new IllegalArgumentException("pduList must not be null or contain nulls");
42        }
43        mPduList = pduList;
44    }
45
46    /**
47     * Returns the contents of a possibly multi-part SMS.
48     *
49     * @return the list of PDUs
50     */
51    public @NonNull List<byte[]> getPdus() {
52        return mPduList;
53    }
54
55    @Override
56    public int describeContents() {
57        return 0;
58    }
59
60    @Override
61    public void writeToParcel(Parcel dest, int flags) {
62        if (mPduList == null) {
63            dest.writeInt(NULL_LENGTH);
64        } else {
65            dest.writeInt(mPduList.size());
66            for (byte[] messagePdu : mPduList) {
67                dest.writeByteArray(messagePdu);
68            }
69        }
70    }
71
72    /**
73     * Constructs a {@link MessagePdu} from a {@link Parcel}.
74     */
75    public static final Parcelable.Creator<MessagePdu> CREATOR
76            = new Parcelable.Creator<MessagePdu>() {
77                @Override
78                public MessagePdu createFromParcel(Parcel source) {
79                    int size = source.readInt();
80                    List<byte[]> pduList;
81                    if (size == NULL_LENGTH) {
82                        pduList = null;
83                    } else {
84                        pduList = new ArrayList<>(size);
85                        for (int i = 0; i < size; i++) {
86                            pduList.add(source.createByteArray());
87                        }
88                    }
89                    return new MessagePdu(pduList);
90                }
91
92                @Override
93                public MessagePdu[] newArray(int size) {
94                    return new MessagePdu[size];
95                }
96            };
97}
98