blob: 19c41b1298e6ed6205228b26f53f34cf21863d6b [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
Cheuksan Wangb72eb972014-11-21 16:54:02 -080017package android.service.carrier;
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070018
Abhijith Shastryd7a305b2014-11-14 10:29:21 -080019import android.annotation.NonNull;
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070020import 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 /**
Abhijith Shastryd7a305b2014-11-14 10:29:21 -080035 * Constructs a MessagePdu with the list of message PDUs.
36 *
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070037 * @param pduList the list of message PDUs
38 */
Abhijith Shastryd7a305b2014-11-14 10:29:21 -080039 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 }
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070043 mPduList = pduList;
44 }
45
46 /**
47 * Returns the contents of a possibly multi-part SMS.
48 *
49 * @return the list of PDUs
50 */
Abhijith Shastryd7a305b2014-11-14 10:29:21 -080051 public @NonNull List<byte[]> getPdus() {
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070052 return mPduList;
53 }
54
55 @Override
56 public int describeContents() {
57 return 0;
58 }
59
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070060 @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 */
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070075 public static final @android.annotation.NonNull Parcelable.Creator<MessagePdu> CREATOR
Cheuksan Wangf9c50c42014-10-21 15:58:23 -070076 = 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}